50
AutoCAD programming Peter Iványi

Lecture 1 Wo Pict

Embed Size (px)

Citation preview

AutoCAD programming

Peter Iványi

Not only graphics

• Lord of the Ring

• MASSIVE – behaviour simulator of masses

Lisp programming language

• 1960, John McCarthy, Massachusetts Institut of Technology

• LISt Processing

• Fortran

• Artificial intelligence research

• Programable programming language

Lisp programming language

• Procedural language(FORTRAN, C, PASCAL, BASIC): a sequence of instructions– Common instruction: store a value in a variable

• Functional language(Lisp): using the definitions of functions– A sequence of functions are evaluated

Lisp and graphic systems

• Object modelling and CAD systems– Allegro Solid + Common Lisp

– ACIS + Scheme

– AutoCAD + AutoLisp

• Flight ticket booking software

• Business decision software

AllegroSolid 1.

AllegroSolid 2.

AllegroSolid 3.

ABUSE game

Artificial intelligence• Representation of

knowledge

• Program of

Harold Cohen : AARON

AutoCAD „programming”

• AutoCAD is a base CAD system

• Every aspect of it can be changed– ActiveX

– Visual Basic

– AutoLisp and Visual Lisp

– ObjectARX

– Menu

– DIESEL

AutoCAD and ActiveX

• Microsoft COM (Component Object Model) technology

• Sharing of drawings and objects with Word and Excel

• Automating tasks

• Several programming languages can be used– Visual Basic

– Java

– C++

AutoCAD ActiveX Automation Interface

AutoCAD and ObjectARX

• AutoCAD Runtime Extension

• DLL (Dynamic Link Library)

• C and C++ programming languages

• Can modify the AutoCAD objects directly

AutoLisp

• Increases the efficiency of the usage of AutoCAD

• Can modify the system variables

• Can modify drawings

• The oldest programming interface of AutoCAD

• Several AutoCAD commands are written in AutoLisp

ObjectARXvoid chngAtt()

{

ads_name entres;

ads_point ptres;

AcDbObjectId _Id, _attId;

AcDbObjectIterator *pIttr = NULL;

if(acedEntSel("Select a Block Reference",

entres, ptres) != RTNORM )

{

//Selection failed

return;

}

acdbGetObjectId(_Id, entres);

AcDbObjectPointer pRef(_Id,AcDb::kForRead);

if(pRef.openStatus()!=Acad::eOk)

{

//Open failed

return;

}

ObjectARXpIttr = pRef->attributeIterator();

while(!pIttr->done())

{

_attId = pIttr->objectId();

AcDbObjectPointer pAtt(_attId,AcDb::kForWrite);

if(pAtt.openStatus()==Acad::eOk)

{

pAtt->setTextString("We changed this");

break;

}

pIttr->step();

}

delete pIttr;

}

Visual BasicOption Explicit

Sub chngAtt()

Dim objEnt As AcadObject

Dim objRef As AcadBlockReference

Dim varAtts As Variant

Dim objAtt As AcadAttributeReference

Dim emptyPt As Variant

ThisDrawing.Utility.GetEntity objEnt, emptyPt, "Sel ect Block: „

If objEnt.ObjectName = "AcDbBlockReference" Then

Set objRef = objEnt

If objRef.HasAttributes Then

varAtts = objRef.GetAttributes

Set objAtt = varAtts(0)

objAtt.TextString = "We changed this"

End If

End If

End Sub

AutoLisp(defun C:chngAtt (/ Mainent entList entAtt entNewAttVal)

(setq Mainent (entsel))

(setq entList (entget (car Mainent)))

(setq entAtt

(entget (entnext (cdr (assoc -1 entList)))))

(setq entNewAttVal (subst (cons 1 "We changed this")

(assoc 1 entAtt) entAtt) )

(entmod entNewAttVal)

(entupd (car Mainent))

(princ)

)

Visual Lisp

• A new generation of AutoLisp

• Not fully compatible– Certain symbols cannot be overdefined

– Functions are not lists

• Development environment– Editor

– Syntax checking and syntax coloring

– Debugger to find problems

– Management of several files

Lisp

• Language of Infinite Stupid Parantheses

; comment

(elem1

(elem2

(elem3 elem4)

(elem5 ‘(elem6 . elem7))

)

)

Lisp comments

• Simple comment:

semi-column ( ; ) until the end of the line

• Comment in several lines:

;| text |;

Lisp comment exampl

; comment in a line

(elema elemb elemc) ; another comment

(elem1 ;| comment |; (elem2 elem3))

(elem4 ;| comment

in several line,

it can be even AutoLISP (elem1) |; elem5)

Characters of the language

Special characters( Starting bracket) End bracket. Point‘ Single apostrof; Semi-column

• Letters, numbers, symbols

• Separation characters

• SPACE, newline, TAB

• Special characters

• Double apostrof (”), string

• Control characters: backslash ( \ )

„non-printable characters”,

Special characters

Meaning\\ Backslash ( \ ) character\” Double apostrof in a string\e Escape character\n newline character\r Return character\t TAB character

\nnn nnn octal character

Example

( elem1 ” adat: \” 4\”\n ”)

( elem2 elem3 . elem4 )

( elem5 ; comment

elem6 elem7

‘( elem8 elem9 )

)

Basic components of AutoLisp

• To describe the program and the data a single data structure is used: symblic expression, S-expression

• John Carthy

Basic components of AutoLisp

– Lists• Series of atoms enclosed by brackets

– Atoms• Definition:

– „everything that is not a list”,

– Cannot be divided any further by the language

• Konstant atoms

• Symbolic atoms

– Special atomsnil, t

Konstant atoms

• The value of the atom is the atom itself.– Numbers: integer, real number

1234, 56.78, 123.56e -3

– Strings”abcdefghijk...”

– Selection set

– Entity name

– File descriptor

Symbolic atoms

• Symbolic atoms = Variables

• The name of the variable starts with a letter, can be followed by any character, except special characters– Example: point1, point2, full_length

• The variable denotes a value, it stores a value

Special atoms

• Cannot be overwritten

• t – true value

• nil – false value and empty list ()

(atom and list at the same time !!!)• pi – 3.141592

• pause – ”\\”

Lists• Enclosed by brackets, separated by white

spaces

• Can be embedded into another list

• There is no limit for this embedding• Empty list: () or nil

( 1 2 3 )

( 1 ( 2 3 ) ( 4 ( 5 6 ) ) )

( 1 ”text” 145.67 )

Wronglists

• (ez (nincs befejezve)

• (ez mar)(ket lista)

• (tul (sok (a)) bezaro) zarojel)

• )hibasan kezdodik a lista)

• hianyzik a kezdo zarojel

• (ez egy list (es egy)) atom

Type of lists

• Data list: (123 345.56 ”qwerty” 78e-6)

• Point list:

– 2D: (0.0 1.0)

– 3D: (12.3 45.6 0.0)

• Function list: ( + 3 4)

( setq qq 1.2)

Evaluation

• LISP = processing of lists

• Evaluation

• AutoLISP is an interpreted

• Heart of the interpreter = evaluator

• Interactive talk to the system

Evaluation cycle

1. Read an S-expression

2. Evaluation of an S-expression• Determine its value

• There can be side effects

3. Print the value and goto step 1

Evaluation cycle

• Every step can be denoted by a LISP function: READ, EVAL, PRINT– read-eval-printcycle

• The prompt while readingParancs:

Command:

Order of evaluation1. Value of a constant atom: itself

2. Value of symbolic atom: the value stored in the variable

3. If it is a list, evaluate it as a function

AutoLisp functions

• General form of a function:( operation A ... B)

there is no question what to do

• Rule of precedence in mathematics:3 + 5 * 4

• Must be learned and practiced

Lets start programming!• Simplest computer is a calculator

• Simple calculations:• ( + 5 5)

• ( + -5 5)

• ( + 5 -5)

• ( - 5 5)

• ( * 3 4)

• ( / 8 12)

Evaluation of functions

• Order of evaluation from left to right

• First evaluate the arguments then the full expression

• The result of the evaluation is also an S-expression: atom or list

• Data and function lists are the same (Neumann theorem)

Embedding of calculations

• The argument of operations cane be number or another calculation

• In this case, first the inner most expression is evaluated

Order of evaluation1.

( * ( + 2 2) ( / ( * ( + 3 5) ( / 30 10)) 2))

= ( * 4 ( / ( * 8 3) 2))

= ( * 4 ( / 24 2))

= ( * 4 12)

= 48

Order of evaluation2.

( + 2 ( / 12 4) ( * 2 2))

• Evalutaion of arguments1. 2 = 2

2. ( / 12 4) = 3

3. ( * 2 2) = 4

• Evaluation of the expression

( + 2 3 4) = 9

Order of evaluation3.

( + 2 ( / 5 10))

• Evaluation of arguments1. 2 = 2

2. ( / 5 10) = 0 !!!

• Evaluation of the expression

( + 2 0) = 2

• If all arguments are integer then the result is an integer!!!

Order of evaluation4.

( + 2 ( / 5.0 10))

• Evaluation of arguments1. 2 = 2

2. ( / 5.0 10) = 0.5 !!!

• Evaluation of the expression

( + 2 0.5) = 2.5

To avoid the evaluation

• To handle the expression as-is:– ( quote (1 2 3)) = (1 2 3)

– ‘ (1 2 3) = (1 2 3)

– (1 2 3) = error

To use AutoCAD commands• To execute an AutoCAD command from

AutoLISP

( command argument s ... )

• To draw a horiszontal line from 0,0• ( command ”vonal ” ”0,0” ”1,0” ””)

• Empty string (”” ) : a SPACE or ENTER

• We will return to this

Definition of functions( defun symbolum (arg1 arg2 ...)

expressions

...

)

– symbolum – the name of the function

– arg1, arg2, ... – arguments

– expressions – sequence of commands to evaluate

• We will return to this

Example( defun xy ()

( command ”vonal ” ”0,0” ”1,0” ””)

( command ”vonal ” ”0 ,0” ”0,1” ””)

)