Freeze the Label's layer of a Civil 3D Object
03 Feb 2016Many times I’ll need to freeze Civil 3D labels found in referenced drawings (XREF). Unfortunately, the layer assigned to the label may not match the object’s layer. This means the AutoCAD LAYFRZ command will not freeze the expected layer.
To remedy this, we need to first access the object data containing the label. We can do this with the nentselp AutoLISP function. You can read up on the documentation to see how it works.
Once we have the object selected, we can query it to see what type we are working with. Using the ObjectName property, we can then retrieve the label’s layer.
For example, the LableStyle property references the label object of a COGO point.
(and (= (op:property oPicked 'ObjectName) "AeccDbCogoPoint")
(vlax-property-available-p oPicked "LabelStyle")
)
Select allThe Note object also uses the LabelStyle property reference.
(and (= (op:property oPicked 'ObjectName) "AeccDbNoteLabel")
(vlax-property-available-p oPicked "LabelStyle")
)
Select allA Parcel object uses the AreaLabelStyle property to reference the label.
(and (= (op:property oPicked 'ObjectName) "AeccDbFace")
(vlax-property-available-p oPicked "AreaLabelStyle")
)
Select allWhile the Parcel Segment labels use the LineLabelStyle and the CurveLabelStyle.
Now that we have the label style reference, we need to explore the actual style. We can find the assigned layer several levels down.
(defun _LabelPropertyValue (VLA-Object /)
;;_ Retrieve label name from label style
;;_ Copyright © 2013-2015 by Richard Lawrence
;;_ Returns string value
(if
(and
(vlax-property-available-p VLA-Object "LabelProperties")
(vlax-property-available-p
(setq VLA-Object
(op:property VLA-Object 'LabelProperties)
)
"Layer"
)
(vlax-property-available-p
(setq VLA-Object (op:property VLA-Object 'Layer))
"Value"
)
)
(op:property VLA-Object 'Value)
)
)
Select allI prefer to only freeze the layer in the viewport. This allows me to still view it in model space, as needed.
(defun _FreezeLabelLayer
(sStyleType lstStyleData blnFreezeStyle / msg)
;;_ Freeze label layer
;;_ Copyright © 2013-2015 by Richard Lawrence
(if (= (type lstStyleData) 'LIST)
(progn
(foreach n lstStyleData
(if
(getvar 'tilemode)
(vla-put-freeze (op:layer (nth 0 n)) :vlax-true)
(vl-cmdf "_vplayer"
"f"
(nth 0 n)
"Current"
""
)
)
(setq msg (strcat "\n"
sStyleType
" Label Style \""
(nth 1 n)
"\""
"\n"
sStyleType
" Label Layer \""
(nth 0 n)
"\" was frozen. "
)
)
)
)
)
(if msg
(princ msg)
)
)
Select allYou can download the completed code below.
;|
ObjectLayerFreeze.lsp
Version History
1.1 September 10, 2015 Added additional Civil 3D object types
1.0 July 11, 2013 Initial Release
Freezes the label's assigned layer of a selected Civil 3D object
Dependencies: none
Usage: (CLI) OLF
Arguments: none
Returns: none
Copyright © 2013-2015 by Richard Lawrence
Written permission must be obtained to copy, modify, and distribute
this software. Permission to use this software for any purpose and
without fee is hereby granted, provided that the above copyright
notice appears in all copies and that both the copyright notice and
the limited warranty and restricted rights notice below appear in
all supporting documentation.
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 c:olf (/ _LabelPropertyValue _FreezeLabelLayer
oPicked sObject sLayer
sName oStyle
)
;;_ Sub Routine definitions
;;_ Retrieve label name from label style
(defun _LabelPropertyValue (VLA-Object /)
;;_ Returns string value
(if
(and
(vlax-property-available-p VLA-Object "LabelProperties")
(vlax-property-available-p
(setq VLA-Object
(op:property VLA-Object 'LabelProperties)
)
"Layer"
)
(vlax-property-available-p
(setq VLA-Object (op:property VLA-Object 'Layer))
"Value"
)
)
(op:property VLA-Object 'Value)
)
)
;;_ Freeze label layer
(defun _FreezeLabelLayer
(sStyleType lstStyleData blnFreezeStyle / msg)
(if (= (type lstStyleData) 'LIST)
(progn
(foreach n lstStyleData
(if
(getvar 'tilemode)
(vla-put-freeze (op:layer (nth 0 n)) :vlax-true)
(vl-cmdf "_vplayer"
"f"
(nth 0 n)
"Current"
""
)
)
(setq msg (strcat "\n"
sStyleType
" Label Style \""
(nth 1 n)
"\""
"\n"
sStyleType
" Label Layer \""
(nth 0 n)
"\" was frozen. "
)
)
)
)
)
(if msg
(princ msg)
)
)
;_ Get Active Document object
(defun op:doc ()
(vla-get-activedocument (vlax-get-acad-object))
)
(defun op:property (vlaObject symProperty /)
(if (vlax-property-available-p vlaObject symProperty)
(vlax-get-property vlaObject symProperty)
)
)
;_ Get Layers collection
(defun op:layers ()
(vla-get-layers (op:doc))
)
;_ Get Layer object by name
(defun op:layer (name)
(if (= (type name) 'STR)
(progn
(setq
name (vl-catch-all-apply 'vla-item (list (op:layers) name))
)
(if (vl-catch-all-error-p name)
nil
name
)
)
)
)
;_ Get vla-object from entity
(defun op:object (entity / object)
(cond ((and (= (type entity) 'LIST)
(= (type (car entity)) 'ENAME)
)
(setq ename (car entity))
)
((and (= (type entity) 'LIST)
(assoc -1 entity)
(= (cdr (assoc -1 entity)))
)
(setq ename (cdr (assoc -1 entity)))
)
((= (type entity) 'ENAME)
(setq ename entity)
)
)
(setq
ename (vl-catch-all-apply 'vlax-ename->vla-object (list ename))
)
(if (vl-catch-all-error-p ename)
nil
ename
)
)
;;_ Main function
(if (and (setq oPicked (nentselp "\nSelect label to freeze: "))
(= (type oPicked) 'LIST)
(or
(= 2 (length oPicked))
(= 4 (length oPicked))
)
(setq oPicked (op:object (car oPicked)))
)
(progn
;(setq objLayers (op:layers))
(cond
((= (op:property oPicked 'ObjectName)
"AeccDbParcelSegmentLabel"
)
(setq sObject "Line"
slayer (_LabelPropertyValue
(setq oStyle (op:property oPicked 'LineLabelStyle))
)
sname (op:property oStyle 'Name)
)
(_FreezeLabelLayer sObject (list (list slayer sname)) nil)
(setq sObject "Curve"
slayer (_LabelPropertyValue
(setq oStyle (op:property oPicked 'CurveLabelStyle))
)
sname (op:property oStyle 'Name)
)
(_FreezeLabelLayer sObject (list (list slayer sname)) nil)
)
((and (= (op:property oPicked 'ObjectName) "AeccDbFace")
(vlax-property-available-p oPicked "AreaLabelStyle")
)
(setq sObject "Parcel Area"
slayer (_LabelPropertyValue
(setq oStyle (op:property oPicked 'AreaLabelStyle))
)
sname (op:property oStyle 'Name)
)
(_FreezeLabelLayer sObject (list (list slayer sname)) nil)
)
((and (= (op:property oPicked 'ObjectName) "AeccDbCogoPoint")
(vlax-property-available-p oPicked "LabelStyle")
)
(setq sObject "Point"
slayer (_LabelPropertyValue
(setq oStyle (op:property oPicked 'LabelStyle))
)
sname (op:property oStyle 'Name)
)
(_FreezeLabelLayer sObject (list (list slayer sname)) nil)
)
((and (= (op:property oPicked 'ObjectName) "AeccDbNoteLabel")
(vlax-property-available-p oPicked "LabelStyle")
)
(setq sObject "Note"
slayer (_LabelPropertyValue
(setq oStyle (op:property oPicked 'LabelStyle))
)
sname (op:property oStyle 'Name)
)
(_FreezeLabelLayer sObject (list (list slayer sname)) nil)
)
((and t
(vlax-property-available-p oPicked "Layer")
)
(vlax-put-property
(op:layer (op:property oPicked 'Layer))
'Freeze
:vlax-true
)
(princ (strcat "\nSelected object's layer \""
(op:property oPicked 'Layer)
"\" was frozen. "
)
)
)
)
)
)
(princ)
)
Select allDownloadThis may not work as expected if the assigned layer is “0” in the label style.
If you have any other Civil 3D objects you feel may work with this method, leave me a note in the comments.
If you liked this post, you can share it with your followers or follow me on Twitter!