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

Add C3D Note Label

I recently ran across a post on the Facebook group CAD Managers Unite! inquiring how to place a Civil 3D command on the tool palette with a specific settings.

As with most things Civil 3D, this is not a trivial task.

My first suggestion to do this is to create a drawing with the desired style. This could be done either by using the AutoCAD WBLOCK command, or using the Civil 3D IMPORTSTYLESANDSETTINGS command. This method would allow you to place the drawing onto the Tool Palette for later execution. I would suggest setting the block to explode upon insertion to reduce clutter within the drawing. Once the note label is within the drawing, the contents may need to be edited as needed.

Be aware, if the drawing is created with the WBLOCK command, additional objects may be included if they are referenced in your chosen style.

Another method would be to automate this task with AutoLISP. Fortunately, this is one area of Civil 3D that is partially available to AutoLISP.

We will first need to again reuse some code I previously posted to retrieve the current Civil 3D drawing document. This will allow us to inspect the drawing for the available note label styles.

We will then check the available note label styles. These would be found in the General Note Label Styles property. (I’ve spaced out the property name for readability.) In most AutoCAD collections, the desired item can be found easily with the vla-item If our specified note label style name is present We wouldn’t want to add a new label with a style that doesn’t exist. That may cause a problem down the line.

Get Object Property

(defun OP:GetObjProperty (sSettingsTree oBase / lSettingsTree oFunction)

    (defun _explode (str delim / ptr lst)
      ;; Split a string into a list by delimiter string
      (while (setq ptr (vl-string-search delim str))
	(setq lst (cons (substr str 1 ptr) lst))
	(setq str (substr str (+ ptr 2)))
      )
      (reverse (cons str lst))
    )
    (setq lSettingsTree (_explode sSettingsTree ":"))
    (if	(not oBase)
      (setq oBase (OP:C3DDoc))
    )
    (foreach n lSettingsTree
      (if (vlax-property-available-p oBase n)
	(setq oFunction	(vlax-get-property oBase n)
	      oBase	oFunction
	)
      )
    )
    oFunction
  )
Select all

If the style is available, we need to change the default note label style setting. This setting is buried a bit within the document properties. It can be found at Settings -> General Settings -> Style Settings -> Note Label Style -> Value. Again, I’ve spaced out the property names to aid in readability.

Set Object Property

  (defun OP:SetObjProperty (sSettingsTree oBase value /)
    (setq lSettingsTree (_explode sSettingsTree ":"))
    (setq sSetting (last lSettingsTree))
    (setq sSettingsBranch
	   (_implode (reverse (cdr (reverse lSettingsTree)))
		     ":"
	   )
    )
    (setq eCatchIt (vl-catch-all-apply
		     'vlax-put-property
		     (list (OP:GetObjProperty sSettingsBranch oBase)
			   sSetting
			   value
		     )
		   )
    )
    (if	(vl-catch-all-error-p eCatchIt)
      (princ (vl-catch-all-error-message eCatchIt))
      (OP:GetObjProperty sSettingsTree oBase)
    )
  )
Select All

And now back to the partial availability part. We will use the Command AutoLISP function to call the Civil 3D command of AddNoteLabel.

Add Note Label

;|

OP_AddNoteLabel_1_0.lsp

Version History
1.0.0         October 30, 2018 Initial Release

Dependencies: OP:c3ddoc
              http://justopie.github.io/blog/2016/01/how-to-add-a-point-group-with-autolisp/#c3ddoc
              sparser - Renamed to _explode
              http://forums.augi.com/showthread.php?59912-convert-String-to-List&p=695041&viewfull=1#post695041
Usage:        (OP:AddNoteLabel "Standard")

Arguments:    sNoteLabelStyle
     Type:    String
              A string denoting a note label style name currently in the drawing.

Returns:      nil

Copyright © 2018 by Richard Lawrence

THIS PROGRAM IS PROVIDED "AS IS" AND WITH ALL FAULTS.  ANY IMPLIED 
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE ARE 
HEREIN DISCLAIMED. THERE IS NO WARRANTY THAT THE OPERATION OF THE 
PROGRAM WILL BE UNINTERRUPTED OR ERROR FREE.  USAGE OF THIS PROGRAM 
IS AT YOUR OWN RISK.

|;
(defun OP:AddNoteLabel (sNoteLabelStyle / coll:NoteLabelStyles)

  (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
  )
  (defun OP:GetCollectionItemByName (oCollection sName / oItem)
    (vlax-for n	oCollection
      (if (and (not oItem)
	       (vlax-property-available-p n 'Name)
	       (= (vlax-get-property n 'Name) sName)
	  )
	(setq oItem n)
      )
    )
    oItem
  )
  (defun OP:GetObjProperty (sSettingsTree oBase / lSettingsTree oFunction)

    (defun _explode (str delim / ptr lst)
      ;; Split a string into a list by delimiter string
      (while (setq ptr (vl-string-search delim str))
	(setq lst (cons (substr str 1 ptr) lst))
	(setq str (substr str (+ ptr 2)))
      )
      (reverse (cons str lst))
    )
    (setq lSettingsTree (_explode sSettingsTree ":"))
    (if	(not oBase)
      (setq oBase (OP:C3DDoc))
    )
    (foreach n lSettingsTree
      (if (vlax-property-available-p oBase n)
	(setq oFunction	(vlax-get-property oBase n)
	      oBase	oFunction
	)
      )
    )
    oFunction
  )

  (defun OP:SetObjProperty (sSettingsTree oBase value /)
    (setq lSettingsTree (_explode sSettingsTree ":"))
    (setq sSetting (last lSettingsTree))
    (setq sSettingsBranch
	   (_implode (reverse (cdr (reverse lSettingsTree)))
		     ":"
	   )
    )
    (setq eCatchIt (vl-catch-all-apply
		     'vlax-put-property
		     (list (OP:GetObjProperty sSettingsBranch oBase)
			   sSetting
			   value
		     )
		   )
    )
    (if	(vl-catch-all-error-p eCatchIt)
      (princ (vl-catch-all-error-message eCatchIt))
      (OP:GetObjProperty sSettingsTree oBase)
    )
  )

  (defun _explode (str delim / ptr lst)
    ;; Split a string into a list by delimiter string
    (while (setq ptr (vl-string-search delim str))
      (setq lst (cons (substr str 1 ptr) lst))
      (setq str (substr str (+ ptr 2)))
    )
    (reverse (cons str lst))
  )
  (defun _implode (data delim / str n)
    ;; Join list items into string with delimiter string
    (if	(and (= (type data) 'LIST)
	     (= (type delim) 'STR)
	     (> (strlen delim) 0)
	)
      (foreach n data
	(if str
	  (setq str (strcat str delim n))
	  (setq str n)
	)
      )
    )
  )

  (if
    (OP:GetCollectionItemByName
      (OP:GetObjProperty
	"GeneralNoteLabelStyles"
	nil
      )
      sNoteLabelStyle
    )
     (progn
       (OP:SetObjProperty
	 "Settings:GeneralSettings:StyleSettings:NoteLabelStyle:Value"
	 nil
	 sNoteLabelStyle
       )
       (command "AddNoteLabel" pause "")
     )
     (princ "\nNote Label Style not found. ")
  )
  (princ)
)
Select allDownload

This is intended as a sub-routine. One would need to supply the note label style to use as a string. One could then add this to the Tool Palette multiple times with differing styles.

If you have suggestions to improve this code, leave a comment below.

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

Similar Posts