Accessing Civil 3D objects with autolisp
I have been working with the Civil 3D 2010 API in visual lisp recently, and I thought I would share an example of working with an alignment object. This example has plenty of comments, but basically it shows you how to find a point near the alignment based on a station and offset. Then it does the opposite and shows you how to determine the station and offset, given a point. For clarity, I have left out most of the error checking.
; standard entity selection (setq sel (entsel "\nSelect Alignment: ")) ; get the entity name (setq ent (car sel)) ; get the entity list (setq lst (entget ent)) ; check to make sure the selection was the expected type (if (eq "AECC_ALIGNMENT" (cdr (assoc 0 lst))) ; if so, convert the entity into a VLA-OBJECT (setq obj (vlax-ename->vla-object ent)) ) (if obj (progn ; get the name of the alignment (setq nam (vlax-get-property obj 'Name)) ; get the start station (setq sta1 (vlax-get-property obj 'StartingStation)) ; get the end station (setq sta2 (vlax-get-property obj 'EndingStation)) ; set a couple of variables (setq sta 100.0 off 10.0) ; using the above variables, find this point on the alignment (vlax-invoke-method obj 'PointLocation sta off 'x 'y) ; create an AutoCAD point at this location (entmake (list (cons 0 "POINT")(cons 10 (list x y)))) ; ask the user to pick a point (setq pt1 (getpoint "\nSelect point: ")) ; find the station and offset for this point (vlax-invoke-method obj 'StationOffset (car pt1) (cadr pt1) 'sta 'off) ; inform the user (alert (strcat "The station is " (rtos sta 2 2) "\nThe offset is " (rtos off 2 2))) ) )