AutoCAD VBA PPT

download AutoCAD VBA PPT

of 19

description

PPT

Transcript of AutoCAD VBA PPT

  • CVEV 118/698 AutoCAD VBALecture 1Prof. Mounir MabsoutElsa SulukdjianWalid El Asmar

  • AutoCAD Development System (ADS):Script file (accessed through the command line).Menu programming system.DIESEL (menu statement language).LISt Processing (LISP) language:For AutoCAD, AutoLISP then VisualLISP (AutoCAD 2000).Visual Basic for Applications (VBA):ActiveX technology.Object ARX:ObjectARX programming environment provides an object-oriented C++ application programming interface.Programming for AutoCAD

  • VBA vs AutoLISPAutoLISP runs from command line or menu system.VBA is more powerful in terms of user-interaction and dialog box interfaces.VBA is also more powerful in linking to other Windows programs, I.e. opening applications, exchanging data.

  • VBA vs VBVBA is run from inside the AutoCAD memory pool; thus, the communication is faster.At runtime, VB does not take the monopole of the AutoCAD activity, I.e. you can still manipulate a drawing using the ADS while the VB runs.VB is more powerful in terms of database manipulation.VBA does not have a form load procedure while VB does, I.e. forms need to be re-initialized in VBA.VBA is a subset of VB. Not all controls and properties found in VB are available in VBA.

  • Getting StartedThe VBA IDE is accessed through the AutoCAD Tools menu Macro Visual Basic Editor, or Alt+F11. Main Features:Project Explorer: tree diagram showing modules, forms and AutoCAD Objects in use. Properties Window.User Form Windows.Code Window.Object Browser: tree structure display used to shop for any AutoCAD/VBA object or method (bala TVA).

  • VBA IDE

  • The Object Oriented ConceptVBA is an object oriented programming language.

    Objects are entities characterized by: Methods: subroutines and functions used to manipulate the object.Properties: set of data associated with the object. Methods act directly on those properties.

    Example:Object Line (acLine)Properties Coordinates of start point Method Set [] StartPoint used to change those coordinates.

  • The Object ModelTree structure that shows the hierarchy of objects in the application.Made of: collections of objects and objects.The AutoCAD Application is at the base of the trunk.Ascending procedure required to access an entity.AutoCAD ApplicationDocumentSDocument

    Active DocumentPreferencesModel SpacePaper SpaceOther Collections BlockS Document

    BlockBlockEntities, I.e. lines, Arcs, Shapes, Solids, etcCollections

    Objects

  • Using the Object ModelAfter declaration, each entity has to be assigned a certain location in the Object Model, in order to be manipulated: Dim Circ as AcadCircleset Circ = ThisDrawing.ModelSpace.Addcircle()It is required to trace this location in an ascending manner, I.e from the trunk up to the concerned branch.Later, any entity can be manipulated either directly by its reference name, or by retracing its location: Circ.Color = acBlueorThisDrawing.ModelSpace.Item(0).Color = acBlue

  • About ThisDrawingThisDrawing is a reference name for the active document in the application.Although the AutoCAD Application is closer in level to the roots of the Object Model, it can be manipulated from the active document, I.e:ThisDrawing.ApplicationAutoCAD ApplicationDocumentSActive DocumentThis is not really in contradiction with the regular procedure, for Application in this case is a not really the AutoCAD Application, but only a pointer to it.

    Application PointerPoints toetc

  • ThisDrawing and FriendsSample Code: Dim oAcad as AcadApplication Dim oDocument AcadDocument Dim oPref as AcadPreferences Set oAcad = GetObject( , AutoCAD.Application) set oDocument = oAcad.ActiveDocument set oPref = oAcad.Preferences

    Equivalent Syntaxes:oDocument. ThisDrawing.oAcad. ThisDrawing.Application.oPref. ThisDrawing.Application.Preferences.

  • Entity Collection ObjectsA Collection Object is a set of things all collected into one object.The Collection Object provides a mechanism by which several items, data, or other objects, can be linked together & referenced as a singular item: a Selection Set.A Selection Set can contain any number of entity objects, of any type (lines, blocks, etc.) referencing even more objects.Generally, 3 methods are associated with collections, providing the basic functions needed to Add, Remove or Access items in a collection.

  • Selection Sets CollectionIn the AutoCAD drawing object model, the storage place for all selection sets is called Selection Sets Collection.Selection Sets are referenced by name (string type) inside the collection.Dim S1 as AcadSelectionSetSet S1 = thisDrawing.SelectionSets.Add(S1)

  • Building a Collection of Entity ObjectsMethods to add objects to the selection set:AddItems: appends an entity object to the selection setSelect: adds entity objects using any of the valid selection mechanisms such as the previous selection set or all entity objects in the drawing, in a box, in a fence, and so forth.SelectAtPoint: adds any entity objects found at a given point to the selection set.SelectByPolygon: adds entity objects found inside a polygon area (fence).SelectOnScreen: operator selection of objects to add to the selection set.

  • Building a Collection of Entity Objects - ExamplesSelect an object & add it to a selection set:Dim S2 As AcadSelectionSetSet S2 = ThisDrawing.SelectionSets.Add(S2)AppActivate ThisDrawing.Application.CaptionS2.SelectOnScreenCreate a point object & add it to the selection set:Dim Apoint As AcadPointDim PNT(0 to 2) as DoublePNT(0) = 0# : PNT(1) = 0# : PNT(2) = 0#Set Apoint = ThisDrawing.ModelSpace.AddPoint(PNT)Dim varApoint(0) As AcadPointSet varApoint(0) = ApointS2.AddItems(varApoint)Note: The AddItems() method requires that we supply an array of object references, and it will not accept just one single object.

  • Accessing Selection Set MembersRetrieving selection set members is done using an offset value into the selection set.The 1st member of the selection is at an offset of 0 & the last member is at an offset of (number of objects in the set 1).When iterating through a selection set, the easiest programming strategy is to use the For-Each-Next loop structure.The item() method is best if you know exactly where the object to be manipulated resides.

  • Accessing Selection Set Members - ExampleThe following program will get the entity type and layer of the 1st object in the entity list. It will then change the layers of matching entity types in the remainder of the selection set. Dim MatchIt As ObjectDim Ent As ObjectSet MatchIt = S2.Item(0)For each Ent in S2If ((MatchIt.EntityType = Ent.EntityType))_ And (MatchIt.Layer Ent.Layer)) ThenEnt.Layer = MatchIt.LayerEnt.UpdateEnd IfNext End

  • More Selection Set Methods and PropertiesMethods:Clear: clears the selection set (selection set still exists).Delete: deletes the selection set from the collection, linkage to the set is broken, and any reference to the set will result in an error.Erase: deletes the items from the drawing. Selection set still exists but without any entity objects in it.Highlight: highlights the objects in the set (to show the operator what items have been selected)RemoveItems: removes one or more entity objects from the selection set (objects are still on the drawing though).Update: regenerates the entity objects on the screen.Properties:Count: Number of entity objects in the selection set. Name: Name of the selection set.

  • Whats NextMore ObjectsMore methods