Author Topic: Setting the color of a layer  (Read 2086 times)

0 Members and 1 Guest are viewing this topic.

Keith Brown

  • Swamp Rat
  • Posts: 601
Setting the color of a layer
« on: August 01, 2013, 04:01:21 PM »
So I am trying to modify a routine that I got from BlackBox that works perfectly.  What it does is create a new layer based on the layer of a selected object and the value that is in the LayerAppend variable.  It sets the color of the layer based on a hard coded value.  Here is the routine
 
Code - Auto/Visual Lisp: [Select]
  1. (setq Hanger_LayerColor acWhite)  ;; Set desired hanger layer color here
  2. (setq Hanger_LayerAppend "-Hngr")     ;; Set name to append to duct layer here
  3.  
  4. ;; Set color for the hanger layer
  5.     (vla-add
  6.         (vla-get-layers
  7.          )
  8.  
  9.         ;; set desired layer name as global variable
  10.         (setq *Duct_layerName*
  11.           (strcat
  12.             (cdr (assoc 8 (entget (ssname ss 0))))
  13.             Hanger_LayerAppend
  14.            )
  15.          )
  16.      )
  17.  
  18.     Hanger_LayerColor
  19. )
  20.  

What I would like to do is modify the routine so that if the value of Hanger_LayerColor is nil then it takes the color of the object layer.  So this is what I thought would work.
 
Code - Auto/Visual Lisp: [Select]
  1. (setq Hanger_LayerColor acWhite)  ;; Set desired hanger layer color here
  2. (setq Hanger_LayerAppend "-Hngr")     ;; Set name to append to duct layer here
  3.  
  4. ;; Set color for the hanger layer
  5.     (vla-add
  6.         (vla-get-layers
  7.          )
  8.  
  9.         ;; set desired layer name as global variable
  10.         (setq *Duct_layerName*
  11.           (strcat
  12.             (cdr (assoc 8 (entget (ssname ss 0))))
  13.             Hanger_LayerAppend
  14.            )
  15.          )
  16.      )
  17.  
  18.     (if (= Hanger_LayerColor nil)
  19.        (cdr (assoc 62 (entget (ssname ss 0))))
  20.        (Hanger_LayerColor)
  21.      )
  22. )
  23.  

But this doesn't work and only generates an error  ** Error: bad function: 7 **.
 
I am an extreme newbie when it comes to lisp and I feel like this should be an easy thing to code.  I am just stuck.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Setting the color of a layer
« Reply #1 on: August 01, 2013, 05:00:21 PM »
After a quick glance, change:
Code - Auto/Visual Lisp: [Select]
  1. (Hanger_LayerColor)
to:
Code - Auto/Visual Lisp: [Select]
  1. Hanger_LayerColor

Also note that DXF Group 62 will not be present for entities with colour set to ByLayer.

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Setting the color of a layer
« Reply #2 on: August 01, 2013, 05:06:40 PM »
After doing some more research that was one of the first things that I did.  I am now getting the error that the activex server returned an error: Parameter not optional.  This leads me to believe that I am not getting the color of the object correctly.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Setting the color of a layer
« Reply #3 on: August 01, 2013, 05:22:43 PM »
I did some more testing and the error is definitely in the code where I am trying to grab the color of the object. 
So......... Now I am trying to figure out how to get the color of the first item in a selection set.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Setting the color of a layer
« Reply #4 on: August 01, 2013, 05:29:19 PM »
Hint:
Code: [Select]
_$ (vla-put-color <VLA-Layer-Object> nil)
; error: ActiveX Server returned an error: Parameter not optional

Also note that DXF Group 62 will not be present for entities with colour set to ByLayer.

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Setting the color of a layer
« Reply #5 on: August 01, 2013, 05:36:14 PM »
Maybe throw in another (if ... ) around the (cdr ...) at the end, such that if it *is* nil it will get the layer of that object and extract the ACI value.

I wonder if we can still assign acBylayer/"BYLAYER" as a color of a layer... I know it can be done with line types.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Setting the color of a layer
« Reply #6 on: August 01, 2013, 05:47:59 PM »
I decided to go another route and instead of getting the color of the object I went with getting the color of the layer that the object resides on.  I think that this will work for my purposes.  Here is the code.
 
Code - Auto/Visual Lisp: [Select]
  1. (setq Hanger_LayerColor acWhite)  ;; Set desired hanger layer color here
  2. (setq Hanger_LayerAppend "-Hngr")     ;; Set name to append to duct layer here
  3.  
  4. ;; Set color for the hanger layer
  5.     (vla-add
  6.         (vla-get-layers
  7.          )
  8.  
  9.         ;; set desired layer name as global variable
  10.         (setq *Duct_layerName*
  11.           (strcat
  12.             (cdr (assoc 8 (entget (ssname ss 0))))
  13.             Hanger_LayerAppend
  14.            )
  15.          )
  16.      )
  17.  
  18.     (if (= Hanger_LayerColor nil)
  19.        (cdr (assoc 62 (entget (tblobjname "LAYER" (cdr (assoc 8 (entget (ssname ss 0))))))))  ;; Get the color of the object layer
  20.        Hanger_LayerColor
  21.      )
  22. )
  23.  

So if I change the value of Hanger_LayerColor to nil the new layer will have the color of the objects layer.  If I leave it alone the new layer will have a white color.  Exactly what I wanted.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013