Author Topic: CAD Setup Routine  (Read 63199 times)

0 Members and 5 Guests are viewing this topic.

amgirard2003

  • Guest
CAD Setup Routine
« Reply #75 on: January 28, 2004, 09:36:59 AM »
O.k i see the problem and i fixed it..

it should be like this
Code: [Select]

(setq clay (getvar "clayer"))



I realized what the problem was..

The (getvar) only accepts one arguement and that arguement is the variable you want to get.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
CAD Setup Routine
« Reply #76 on: January 28, 2004, 09:37:31 AM »
Quote from: Daron
Also, don't forget to test each line of code


Andre'

Have you tried the trace process in VLIDE??

Did you have any questions on that?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

amgirard2003

  • Guest
CAD Setup Routine
« Reply #77 on: January 28, 2004, 09:48:21 AM »
Quote from: CAB
Quote from: Daron
Also, don't forget to test each line of code


Andre'

Have you tried the trace process in VLIDE??

Did you have any questions on that?


I'm trying to remember to test each line of code and to do that i've kept the Watch Window open so it reminds me to test..

If Trace is different than Watch, then i have no idea what you're talking about.. never heard about it?

If it's something that is useful and that will help me out, please direct me in the right direction..

amgirard2003

  • Guest
CAD Setup Routine
« Reply #78 on: January 28, 2004, 11:35:10 AM »
Question for all of you

In the following code that i have

Code: [Select]

(defun newlayers ( / cmd clay) ; defined the function as newlayers with local variables CMD CLAY
  (setq cmd (getvar "cmdecho")) ; set variable cmdecho to cmd for temporary status
  (setq clay (getvar "clayer" )) ; set variable clayer to clay for temporary status
  (setvar "cmdecho" 0) ; set variable cmdecho to 0, to disable commands being echoed to the command line
 
  (command ".layer" "m" "1"    "c"    "1"    ""    "" ; Start Layer command and create the following layers
  ".layer" "m" "2"    "c"    "2"    ""    "" ; ".layers" is the layer command, "m" stands for make
  ".layer" "m" "3"    "c"    "3"    ""    "" ; "1" is the name, "c" is the color, "1" is the color
  ".layer" "m" "4"    "c"    "4"    ""    ""
 ) ; End of the Layer command
  (setvar "clayer" "1") ; set current layer to 1
  (setvar "cmdecho" cmd) ; set cmdecho back to original state with variable cmd
  (setvar "clayer" clay) ; set clayer back to original state with variable clay
  (princ) ; silent exit
) ; end of defun function


How can i apply this part to it..

When you run this code it creates to the following layers.. o.k great..
now after creating these layers i change the color of 2 of the layers and i purge the drawing and it deletes 2 other layers i didn't use...
I then realize "OH CRAP" i need those 2 layers and i re-run the routine to load those 2 layers but i don't want the layers i changed the colors to change??

I assume that i have to apply and IF statement with a variable but i don't know where to begin

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
CAD Setup Routine
« Reply #79 on: January 28, 2004, 12:07:39 PM »
ander'

Tracing or Stepping through the code is what I meant and it looks like
you are doing that.

Your other question on avoiding the re-creation of a layer.
something like this will work.

Code: [Select]

  (if (not (tblsearch "layer" "1"))
    (command ".layer" "m" "1" "c" "1" "" "")
  )
  (if (not (tblsearch "layer" "2"))
    (command ".layer" "m" "2" "c" "2" "" "")
  )
  (if (not (tblsearch "layer" "3"))
    (command ".layer" "m" "3" "c" "3" "" "")
  )
  (if (not (tblsearch "layer" "4"))
    (command ".layer" "m" "4" "c" "4" "" "")
  )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

daron

  • Guest
CAD Setup Routine
« Reply #80 on: January 28, 2004, 12:13:38 PM »
If I were you, I'd take Keith's layer creater routine and pick it apart. Find out how it ticks. What you can do to test it, is for each variable on the left side of "/" in (defun xxx (var1 var2 var3 / lv lv2 lv3) you can write a setq var1 etc and that value will be read when you test the code.

amgirard2003

  • Guest
CAD Setup Routine
« Reply #81 on: January 28, 2004, 12:34:04 PM »
I copied Keith Routine and i'm trying to run it to see what it does but it doesn't work.. (err.. too few arguements)

can someone help me out with getting it to work?

daron

  • Guest
CAD Setup Routine
« Reply #82 on: January 28, 2004, 12:38:44 PM »
That's what I was trying to explain. Arguments! You need to somehow supply values to all the arguments (variables) to the left of the /. You can do a setq and set a value for each, or you could do as it seems you've done and run the code  like so: (functionname arg1 arg2 arg3...). All arg's are the value you specify. So, if you want the layer name to be "andre", you specify that in the layer name's argument space. Does that make any sense?

amgirard2003

  • Guest
CAD Setup Routine
« Reply #83 on: January 28, 2004, 12:41:26 PM »
Thanks Daron.... i get it now...
I'll give it a try...

daron

  • Guest
CAD Setup Routine
« Reply #84 on: January 28, 2004, 12:46:05 PM »
Code: [Select]
(defun mlayer (layer_name aci_layer_color on_off thaw_frz lock_unlk / new_bit layer_list)
  (if (not (tblsearch "LAYER" layer_name))
    (setq layer_list (entget (tblobjname "layer" "0")))
    (setq layer_list (entget (tblobjname "layer" layer_name)))
  )
  (if (= thaw_frz 0)
    (setq new_bit 0)
    (setq new_bit 1)
  )
  (if (= lock_unlk 1)
    (setq new_bit (+ new_bit 4))
  )
  (if (= on_off 0)
    (setq aci_layer_color (- 0 aci_layer_color))
  )
  (setq layer_list (subst (cons 2 layer_name)(assoc 2 layer_list) layer_list))
  (setq layer_list (subst (cons 62 aci_layer_color)(assoc 62 layer_list) layer_list))
  (setq layer_list (subst (cons 70 new_bit)(assoc 70 layer_list) layer_list))
  (if (not (tblsearch "LAYER" layer_name))
    (entmake layer_list)
    (entmod layer_list)
  )
)

Let's look at his code. Load that and on the command line paste or rewrite this:
Code: [Select]
(mlayer "Andre" 6 1 0 0)
You should now have a layer called Andre that is magenta.

amgirard2003

  • Guest
CAD Setup Routine
« Reply #85 on: January 28, 2004, 01:33:12 PM »
I was screwing up the beginning but it's working now..

let's see how i do on breaking this down

Code: [Select]

(defun mlayer (layer_name aci_layer_color on_off thaw_frz lock_unlk / new_bit layer_list)
  (if (not (tblsearch "LAYER" layer_name))
    (setq layer_list (entget (tblobjname "layer" "0")))
    (setq layer_list (entget (tblobjname "layer" layer_name)))
  )


-Define the function as mlayer and the following arguements..
ex. (mlayer "Andre" 4 6 1 0)
- if the layer doesn't exist then go and copy layer 0 as the background for the new layer to create on, and in the following line change the name of the layer you copied to "Andre".

Code: [Select]


(if (= thaw_frz 0)
    (setq new_bit 0)
    (setq new_bit 1)
  )



check and see if you set the layer to be either frozen or thawed.. if set to 0 then it will stay thawed and 1 will set it to frozen

Code: [Select]


(if (= lock_unlk 1)
    (setq new_bit (+ new_bit 4))
  )


check to see if it will be locked or unlocked.. if set to 0 it will stay unlocked and will return NIL if set to 1 it will lock the layer
Question:  why (+ new_bit 4)??

Code: [Select]


(if (= on_off 0)
    (setq aci_layer_color (- 0 aci_layer_color))
  )


check and see if it will be on or off, if set to 0 it will be OFF if set to 1 then it will be turned on.
Question: why (- 0 aci_layer_color)

Code: [Select]

(setq layer_list (subst (cons 2 layer_name)(assoc 2 layer_list) layer_list))
  (setq layer_list (subst (cons 62 aci_layer_color)(assoc 62 layer_list) layer_list))
  (setq layer_list (subst (cons 70 new_bit)(assoc 70 layer_list) layer_list))


O.k the tricky part for me..
Now it's time to take all the variables and put them together..
take Layer name and add it to the layer_list ("Andre")
take Layer color and add it to the layer_list (cyan or 4)
modify all the new bits ( freeze/thaw , On/Off, Lock/Unlock)

question: Confused on the last one (setq layer_list (subst (cons 70 new_bit)(assoc 70 layer_list) layer_list)) ??

Code: [Select]


(if (not (tblsearch "LAYER" layer_name))
    (entmake layer_list)
    (entmod layer_list)



and the for the final part..
If layer is TRUE and EXIST at the beginning then it will become true and will only be modified (entmod layer_list)
(entmake_list) will make the layer if it's FALSE from the beginning..



So how did i do??  B or B+ :)

daron

  • Guest
CAD Setup Routine
« Reply #86 on: January 28, 2004, 02:07:09 PM »
Do you know where to find the dxf Reference in the help files? Look in there, under Tables and select layer. Study that page a bit and you should have your answers.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
CAD Setup Routine
« Reply #87 on: January 28, 2004, 02:08:08 PM »
I would give that a B+ but there are a few minor things that need to be clarified. I will point them out and you will then understand the things you questioned before.
Quote

-Define the function as mlayer and the following arguements..
ex. (mlayer "Andre" 4 6 1 0)
- if the layer doesn't exist then go and copy layer 0 as the background for the new layer to create on, and in the following line change the name of the layer you copied to "Andre".


The last three arguments of this function should only be 0 or 1
The code first checks if the specified layer exists if it does not, then it will grab the contents of layer 0 as a starting point, a template if you will, for the new layer - in this instance "Andre"

Quote

check and see if you set the layer to be either frozen or thawed.. if set to 0 then it will stay thawed and 1 will set it to frozen


To expand upon this a bit, the variable new_bit will hold all of the bitwise values for the layer as defined by DXF code 70, you will need to see the DXF code explanation in the help files for the bit codes. Suffice to say that 1 = freeze

Quote

check to see if it will be locked or unlocked.. if set to 0 it will stay unlocked and will return NIL if set to 1 it will lock the layer
Question: why (+ new_bit 4)??


If the layer is designated to be locked, the bitwise value for a locked layer will be added to new_bit. As before the value of Freeze/Thaw Lock/Unlock as well as a few others we will not be concerned with here are bitwise. The bit for locking happens to be 4. So to lock AND freeze the value would be 4 + 1 or 5 that is why we are adding the 4 to new_bit.

Quote

check and see if it will be on or off, if set to 0 it will be OFF if set to 1 then it will be turned on.
Question: why (- 0 aci_layer_color)


As defined by the program 1 = on and 0 = off, this has nothing to do with the layer actually being on or off, only how we tell what the user wants it to be. The reason for (- 0 aci_layer_color) is that the way AutoCAD determines if a layer is on or off is that the layer color is either a positive number OR a negative number. If the layer color index number is negative the layer will be turned off, to do this, if the user elected to turn off the layer, (on_off = 0) then we subtract the layer index number from 0 effectively giving a negative value and turning the layer off.

Quote

O.k the tricky part for me..
Now it's time to take all the variables and put them together..
take Layer name and add it to the layer_list ("Andre")
take Layer color and add it to the layer_list (cyan or 4)
modify all the new bits ( freeze/thaw , On/Off, Lock/Unlock)

question: Confused on the last one (setq layer_list (subst (cons 70 new_bit)(assoc 70 layer_list) layer_list)) ??


Ok, first we substitute the new layer name layer_name for the old layer name (assoc 2 layer_list) in the layer entity list layer_list and save it back to itself. Then we use the same scenario to substitute the new color for this layer, finally we set the bitwise setting for the frz/thw lck/ulk layer state using the same scenario. Remember that DXF 70 holds the setting for frz/thw and lck/ulk and that is why we modify that setting.

Quote

If layer is TRUE and EXIST at the beginning then it will become true and will only be modified (entmod layer_list)
(entmake_list) will make the layer if it's FALSE from the beginning..


The reason we need this is because if layer "Andre" were to exist in the beginning, then we could not (entmake ...) it, but we could (entmod ...) it, and at the same time if the layer did not exist in the beginning, we could not (entmod ...) it but would have to create it with (entmake ...).

Hopefully this explained it a little better and you have a better understanding of lisp.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

amgirard2003

  • Guest
CAD Setup Routine
« Reply #88 on: January 28, 2004, 02:29:50 PM »
Thanks... that's has cleared up a lot of cobwebs...
I kind of surprised myself, didn't think i would be able to disect
the code... i think i was pretty close...

I wasn't aware of the dxf codes... so that made a lot of things just click..

daron

  • Guest
CAD Setup Routine
« Reply #89 on: January 28, 2004, 02:40:08 PM »
Alright! So, now do you understand how and why to use reusable code and what it means? Remeber, not all modules/functions/reusable code have to have arguments. Those are the easy one's to understand. Do you understand why many will have arguments?