Just notes from Opie A commentary on AutoCAD and Civil 3d

How-to Add a Point Group with AutoLISP

Point Groups For many years, I have tinkered with customizing AutoCAD using AutoLISP. Once I moved to Civil 3D, I was limited in using AutoLISP to continue writing routines to automate tedious tasks. As the Civil 3D has matured, the AutoLISP access to Civil 3D items in the drawings has increased.

Today, I need to create a point group to organize some points in my drawing. The current method OOTB requires one to right-click the Point Groups branch found in the Tool Space. This is one of those tedious tasks, which can’t be automated if it must use the mouse or a dialog box to interact. BTW, you can also create new Point Groups with the CreatePointGroup command.

Fortunately, the Point Groups property can be retrieve from the active Civil 3D document. First, we need to retrieve the active Civil 3D document. To do that, we can use this routine.

Get Active Civil 3D Document Object

(defun OP:c3ddoc (/ prod verstr c3dver)
    (defun c3dver (/ c3d *acad*)
      (setq C3D	(strcat	"HKEY_LOCAL_MACHINE\\"
			(if vlax-user-product-key
			  (vlax-user-product-key)
			  (vlax-product-key)
			)
		)
	    C3D	(vl-registry-read C3D "Release")
	    c3d	(substr
		  C3D
		  1
		  (vl-string-search
		    "."
		    C3D
		    (+ (vl-string-search "." C3D) 1)
		  )
		)
      )
      c3d
    )
    (if	(not _C3DDoc) ;; Check to see if a global variable is set
      (setq
	_C3DDoc	(vla-get-activedocument
		  (vla-getinterfaceobject
		    (vlax-get-acad-object)
		    (strcat "AeccXUiLand.AeccApplication." (c3dver))
		  )
		)

      )
    )
    _C3DDoc  ;; Return reference to active civil 3d document object
  )
Select all

According to some, this may not be the most efficient code to retrieve the active Civil 3D document, but my purpose for this routine is not currently meant to run numerous times at once. Naturally, it is still faster than completing the tasks manually. In the future, I may see where this can be improved without requiring an edit for each release of Civil 3D.

Now that we have a routine to retrieve the active Civil 3D document, we can retrieve the Point Groups property. This would be a simple one line function.

Get Point Groups Property

(vlax-get-property (OP:c3ddoc) 'PointGroups)
Select all

The Point Groups property is actually a collection of the point groups in the drawing. There are many other collections used in AutoCAD and many of it’s verticals. Therefore, I came up with this function to either add a new item to the collection or get the specificly named item from the collection.

Add or Get Item from Collection

(defun addorgetitem (objCollection strName / objFromCollection)
  (or (= (type (setq objFromCollection
              (vl-catch-all-apply
            'vla-add
            (list objCollection strName)
              )
           )
     )
     'VLA-OBJECT
      )
      (= (type (setq objFromCollection
              (vl-catch-all-apply
            'vla-item
            (list objCollection strName)
              )
           )
     )
     'VLA-OBJECT
      )
  )
  (if (= (type objFromCollection) 'VL-CATCH-ALL-APPLY-ERROR)
    (setq objFromCollection nil)
    objFromCollection
  )
)
Select all

Finally, we can pull all of this together to add or get an existing point group.

Add Point Group

(defun AddPointGroup (strName / objGroup objGroups)
  (if (and (setq objGroups (vlax-get-property (op:c3ddoc) 'PointGroups))
	   (setq objGroup (addorgetitem objGroups strName))
      )
    objGroup
  )
)

(defun GetPointGroup (strName / objGroup objGroups)
  (if (and (setq objGroups (vlax-get-property (op:c3ddoc) 'PointGroups))
	   (= 'VLA-OBJECT (type (setq objGroup (vl-catch-all-apply 'vla-item (list objGroups strName)))))
      )
    objGroup
  )
)
Select all

This will return the Point Group object.

If you liked this post, you can share it with your followers or follow me on Twitter!

Similar Posts