All objects in the AutoCAD object model are grouped in collections.
Note: ActiveX support in AutoLISP is limited to Windows only.
For example, the Blocks collection is made up of all blocks in an AutoCAD drawing, and the ModelSpace collection comprises all graphical objects (circles, lines, polylines, and so on) in the drawing's model space.
The following table lists the collections that are part of the AutoCAD object model:
[td]
AutoCAD collection objects |
Block | ModelSpace |
Blocks | PaperSpace |
DimStyles | PlotConfigurations |
Documents | PopMenus |
FileDependencies | RegisteredApplications |
Groups | SelectionSets |
Hyperlinks | TextStyles |
Layers | Toolbars |
Layouts | UCSs |
Linetypes | Viewports |
Materials | Views |
MenuGroups |
|
Retrieving a Member from a CollectionThe Item method is used to retrieve a member object from a collection, while the Count property returns the number of items in a collection. Using the Item method and Count property, you can individually process each object in a collection.
For example, you can look at each object in a model space, determine the type of object, and process only the types of objects you are interested in. The following code prints the start angle for each Arc object in model space:
(setq index 0)(repeat (vla-get-count mspace) (if (= "AcDbArc" (vla-get-objectname (vla-item mspace index))) (progn (princ "\nThe start angle of the arc is ") (princ (vla-get-startangle (vla-item mspace index))) ) ) (setq index (+ index 1)))Note: Item and Count also apply to groups and selection sets.
Applying a Function to All Items in a CollectionYou can use vlax-map-collection to apply a single function to every object in a collection. This can be helpful when you want to list the value of a specific property for each object in a collection, such as each member’s name.
The syntax for the function is:
VLISP 对象集合
Note: The preceding example does not show every property returned by vlax-dump-object.
Applying Multiple Expressions to All Items in a CollectionYou can use vlax-for to evaluate a series of functions with each object in a collection. This function is much more flexible than using vlax-map-collection. Like the foreach function, vlax-for returns the result of the last expression evaluated inside the for loop.
Note: Modifying the collection (that is, adding or removing members) while iterating through it may cause an error.
The syntax for the function is:
(vlax-for symbolcollection [expressions] ...)The following example defines a function that uses vlax-for to show color statistics for each object in the active drawing: