Jump to content
guitarguy1685

Order of function?

Recommended Posts

guitarguy1685

I have the following lisp

 

(defun C:WD (/)
 (setq CRlayr (getvar "CLAYER"))
 (setq CRecho (getvar "CMDECHO"))
;-------------------------------------------------------------------------
   (setq *error*            ;;;resets sysvar incase of
    (lambda (msg)            ;;;an error, esc, exit, cancel
      (setvar "CMDECHO" CRecho)
      (setvar "CLAYER" CRlayr)
      (princ msg)
      (princ)))
;---------------------------------------------------------------
 
 (if (tblsearch "DIMSTYLE" "standard")
     (command "_.dimstyle" "r" "Standard")
     (command "_.dimstyle" "s" "Standard"))

 
 (if (tblsearch "layer" "dimension")
     (progn
[color=Red]    (command "_.layer" "s" "dimension" "")
   (command "_.dimlinear"))[/color]
     (progn
   (command "_.layer" "N" "dimension" "c" "1" "dimension" "s" "dimension" "")
   (command "_.dimlinear")))
 (setvar "CLAYER" CRlayr)
 (setvar "CMDECHO" CRecho)
 (princ)
 )

I basically want to change the layer to "dimension" layer then draw the dimensions. but when I draw the dimensions they are not drawn on the dimension layer.

 

Why will the layer not change before it goes to the 2nd command "dimlinear"?

Share this post


Link to post
Share on other sites
MSasu

Why don’t you use

(setvar "CLAYER" "dimension")

instead of command call?

Share this post


Link to post
Share on other sites
BearDyugin
Why don’t you use

(setvar "CLAYER" "dimension")

instead of command call?

If such layer is not present?

 

guitarguy1685, There are many ways of the creation of layers, for example entmakex or vla-add. But "there is a time for all things".

In yours cases, I would use

[b][color="Red"]([/color][color="Blue"]VL-CMDF[/color] [color="Magenta"]"_-LAYER" "_M" "dimension" ""[/color][color="red"])[/color][/b]

if the layer is not present, he will create it,and make active, if the layer is already present, it will make it active

Share this post


Link to post
Share on other sites
MSasu
If such layer is not present?

 

I was talking about his issue (the code in red); that case (layer not available) was already covered in the code. But, for sure, wasn’t clear enough on this in my post.

Share this post


Link to post
Share on other sites
David Bethel

It actually takes a good bit of code to ensure that you are set to a given layer:

[b][color=BLACK]([/color][/b]defun SetLayer [b][color=FUCHSIA]([/color][/b]name / ldef flag[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]command [color=#2f4f4f]"_.LAYER"[/color][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]if [b][color=NAVY]([/color][/b]not [b][color=MAROON]([/color][/b]tblsearch [color=#2f4f4f]"LAYER"[/color] name[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]command [color=#2f4f4f]"_Make"[/color] name[b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]progn
       [b][color=MAROON]([/color][/b]setq ldef [b][color=GREEN]([/color][/b]tblsearch [color=#2f4f4f]"LAYER"[/color] name[b][color=GREEN])[/color][/b]
             flag [b][color=GREEN]([/color][/b]cdr [b][color=BLUE]([/color][/b]assoc 70 ldef[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
       [b][color=MAROON]([/color][/b]and [b][color=GREEN]([/color][/b]= [b][color=BLUE]([/color][/b]logand flag  1[b][color=BLUE])[/color][/b]  1[b][color=GREEN])[/color][/b]
            [b][color=GREEN]([/color][/b]command [color=#2f4f4f]"_Thaw"[/color] name[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
       [b][color=MAROON]([/color][/b]and [b][color=GREEN]([/color][/b]minusp [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 62 ldef[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
            [b][color=GREEN]([/color][/b]command [color=#2f4f4f]"_On"[/color] name[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
       [b][color=MAROON]([/color][/b]and [b][color=GREEN]([/color][/b]= [b][color=BLUE]([/color][/b]logand flag  4[b][color=BLUE])[/color][/b]  4[b][color=GREEN])[/color][/b]
            [b][color=GREEN]([/color][/b]command [color=#2f4f4f]"_Unlock"[/color] name[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
       [b][color=MAROON]([/color][/b]and [b][color=GREEN]([/color][/b]= [b][color=BLUE]([/color][/b]logand flag 16[b][color=BLUE])[/color][/b] 16[b][color=GREEN])[/color][/b]
            [b][color=GREEN]([/color][/b]princ [color=#2f4f4f]"\nCannot Set To XRef Dependent Layer"[/color][b][color=GREEN])[/color][/b]
            [b][color=GREEN]([/color][/b]quit[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
       [b][color=MAROON]([/color][/b]command [color=#2f4f4f]"_Set"[/color] name[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]command [color=#2f4f4f]""[/color][b][color=FUCHSIA])[/color][/b]
 name[b][color=BLACK])[/color][/b]

 

-David

Share this post


Link to post
Share on other sites
guitarguy1685

hmmm i don't quite understand what "logand" function does. AutoLisp help files reads

 

logand Returns the result of the logical bitwise AND of a list of integers

(logand [int int...])

Arguments

int An integer.

 

Return Values

An integer (0, if no arguments are supplied).

Examples

Command: (logand 7 15 3)

3

Command: (logand 2 3 15)

2

Command: (logand 8 3 4)

0

I don't understand the "result of logical bitwise". can you explain?

 

thx for the replies btw. I love to learn/use new autolisp functions

Share this post


Link to post
Share on other sites
guitarguy1685

nevermind i found an explanation another thread. I still don't understand it but I'm going to reply there.

Share this post


Link to post
Share on other sites
Lee Mac

Quick summary,

 

Bit-codes can be combined using addition, and logand/logior can be used to "dismantle" these combinations among other things.

 

For example, say you have a bit code:

 

(+ 1 4 8 32) = 45

logand will return the bit-code using an "AND" logical statement, i.e. if both codes contain it:

 

(logand      4  45) =  4

(logand (+ 4  45) = 12 ==> as [b]both[/b] are present

(logand (+ 2  45) =  8  ==> as 2 is [b]not[/b] present

logior will return the bit-code using an "Inclusive OR" logical statement, i.e. the bits that are contained in one or the other, or both:

 

(logior       4 45) = 45

(logior (+ 4  45) = 45

(logior (+ 2  45) = 47  ==>  2 [b]not contained[/b] in 45, but [b]included[/b] using[b] IOR[/b]

In addition to these, we can create a logxor function, using an "Exclusive OR" logical statement, i.e. the bits that are contained in one or the other, but not both.

 

(defun logxor (int1 int2) (- (logior int1 int2) (logand int1 int2)))

Example:

 

(logxor       4 45) = 41

(logxor (+ 4  45) = 33

(logxor (+ 2  45) = 39

You can think of each of these in terms of binary, in which 1 = true and 0 = false, hence:

 

(logand (+ 2  45)

Decimal Equivalent     32 16  8  4  2  1
------------------------------------------------ 
                        [b]1  0  [color=Blue]1[/color]  1  0  1[/b]   (45)
                        [b]0  0  [color=Blue]1[/color]  0  1  0[/b]   (10)
[b]logand[/b] -----------------------------------------
                        [b]0  0  [color=Blue]1[/color]  0  0  0[/b]    (
------------------------------------------------

(logior (+ 2  45)

Decimal Equivalent     32 16  8  4  2  1
------------------------------------------------ 
                       [b][color=Blue]1[/color]  0  [color=Blue]1[/color]  [color=Blue]1[/color]  0  [color=Blue]1[/color][/b]   (45)
                       [b]0  0  [color=Blue]1[/color]  0  [color=Blue]1[/color]  0[/b]   (10)
[b]logior[/b] -----------------------------------------
                       [b][color=Blue]1[/color]  0  [color=Blue]1[/color]  [color=Blue]1[/color]  [color=Blue]1  1[/color][/b]   (47)
------------------------------------------------

(logxor (+ 2  45)

Decimal Equivalent     32 16  8  4  2  1
------------------------------------------------ 
                       [b][color=Blue]1[/color]  0  1  [color=Blue]1[/color]  0  [color=Blue]1[/color][/b]   (45)
                       [b]0  0  1  0  [color=Blue]1[/color]  0[/b]   (10)
[b]logxor[/b] -----------------------------------------
                       [b][color=Blue]1[/color]  0  0  [color=Blue]1[/color]  [color=Blue]1[/color]  [color=Blue]1[/color] [/b]  (39)
------------------------------------------------

Hope this helps!

Share this post


Link to post
Share on other sites
guitarguy1685

it helps alot. It's funny how I ask one question and get side tracked another other Lisp adventures lol. Well let me get back to what I really wanted to know. In my code I originally posted

 

[color=Red]    (command "_.layer" "s" "dimension" "")
   (command "_.dimlinear"))[/color]

it seems as though the the layer SHOULD be set after the line but it doesn't take effect. Why wouldn't the layer change before the dimlinear command?

 

The funny thing is if insert 2 pauses

 

[color=Red]    (command "_.layer" "s" "dimension" "")
   (command "_.dimlinear" pause pause))[/color]

then the layer is changed. what's going on here?

Share this post


Link to post
Share on other sites
flowerrobot

To answer you question. It seems to run sweat on Acad 2010.

 

But as the others suggested try some thing like

(defun C:WD (/ CRlayr CRecho *error*) ;Always localise
 (setq CRlayr (getvar "CLAYER"))
 (setq CRecho (getvar "CMDECHO"))
 (setvar "CMDECHO" 0)  ;If you going to store it, Change it
;-------------------------------------------------------------------------
   (defun *error*  (msg)        ;The other way scares me 
      (setvar "CMDECHO" CRecho)
      (setvar "CLAYER" CRlayr)
      (princ msg)
      (princ))
;---------------------------------------------------------------

(if (tblsearch "DIMSTYLE" "standard")
  (vl-cmdf "_.dimstyle" "r" "Standard")
  (vl-cmdf "_.dimstyle" "s" "Standard")
)
(if (not (tblsearch "layer" "dimension"))
   ;A more efficent way to make a layer - Look at DFX in help to lear what the numbers mean
   ;(entmake '((0 . "LAYER")(100 . "AcDbSymbolTableRecord")(100 . "AcDbLayerTableRecord")(70 . 0)(2 . "dimenssions")(62 . 1)(6 . "Continuous")(6 . "Continuous")))
 (vl-cmdf "_.layer" "N" "dimension" "c" "1" "dimension")
)
(setvar "clayer" "dimension")
(command "_.dimlinear")
(setvar "CLAYER" CRlayr)
(setvar "CMDECHO" CRecho)
(princ)
 )


Flower

Share this post


Link to post
Share on other sites
David Bethel
[color=Red]    (command "_.layer" "s" "dimension" "")
   (command "_.dimlinear"))[/color]

 

Just for kicks try this:

[color=Red]    (command "_.layer" "s" "dimension" "")
   (princ)
   (command "_.dimlinear"))[/color]

 

In 2000 and later, you had to add (princ) to a lot of layer commands in Autolisp. I can't remember why, just that I had to redo a ton layer scripts. -David

Share this post


Link to post
Share on other sites
Lee Mac

Try this:

 

(defun C:WD (/ CRlayr CRecho)
 
 (setq CRlayr (getvar "CLAYER"))
 (setq CRecho (getvar "CMDECHO"))

 (setq *error*  (lambda (msg)
                  (setvar "CMDECHO" CRecho)
                  (setvar "CLAYER" CRlayr)
                  (princ msg)
                  (princ)))
 
 (if (tblsearch "DIMSTYLE" "Standard")
   (command "_.-dimstyle" "_r" "Standard")
   (command "_.-dimstyle" "_s" "Standard"))
 
 (if (tblsearch "LAYER" "dimension")
   (progn
     (setvar "CLAYER" "dimension")
     (command "_.dimlinear"))
   (progn
     (command "_.[color=Red][b]-[/b][/color]layer" "_M" "dimension" "_c" "1" "dimension" "_s" "dimension" "")
     (command "_.dimlinear")))
 
 (setvar "CLAYER" CRlayr)
 (setvar "CMDECHO" CRecho)
 (princ))

Share this post


Link to post
Share on other sites
guitarguy1685

thx for the help. The "(princ") or "-layer" didn't seem to work. Only adding 2 pauses in the dimlinear command line makes layer change. It seems like a mickymouse way to do it but it get's the job done. What I hate about it is if I use (cmdecho 0) then the command prompt is empty which looks goofy. So for now I'll leave cmdecho at 1 so you can see a command is in progress.

Share this post


Link to post
Share on other sites
Lee Mac

Another method:

 

(defun c:WD (/ *error* pt1 pt2 pt3)
 (vl-load-com)

 (defun *error* (msg)
   (and uFlag (vla-EndUndoMark *doc))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ))

 (setq *doc (cond (*doc) ((vla-get-ActiveDocument (vlax-get-acad-object)))))

 (or (tblsearch "LAYER" "Dimension")
     (vla-put-color (vla-add (vla-get-layers *doc) "Dimension") 1))

 (if (and (setq pt1 (getpoint "\nSpecify First Point: "))
          (setq pt2 (getpoint "\nSpecify Second Point: " pt1))
          (setq pt3 (getpoint "\nSpecify Alignment Point: " pt1)))

   (vla-put-layer
     (vla-AddDimRotated
       (if (eq acPaperSpace (vla-get-ActiveSpace *doc))
         (if (eq :vlax-true (vla-get-mspace *doc))
           (vla-get-ModelSpace *doc)
           (vla-get-PaperSpace *doc))
         (vla-get-ModelSpace *doc))
       (vlax-3D-point pt1)
       (vlax-3D-point pt2)
       (vlax-3D-point pt3)
       (if (< (abs (- (car pt2)  (car pt1)))
              (abs (- (cadr pt2) (cadr pt1)))) (/ pi 2.) 0.)) "Dimension"))

 (princ))
     

 

Share this post


Link to post
Share on other sites
LEsq
it helps alot. It's funny how I ask one question and get side tracked another other Lisp adventures lol. Well let me get back to what I really wanted to know. In my code I originally posted

 

[color=red]    (command "_.layer" "s" "dimension" "")[/color]
[color=red]   (command "_.dimlinear"))[/color]

it seems as though the the layer SHOULD be set after the line but it doesn't take effect. Why wouldn't the layer change before the dimlinear command?

 

The funny thing is if insert 2 pauses

 

[color=red]    (command "_.layer" "s" "dimension" "")[/color]
[color=red]   (command "_.dimlinear" pause pause))[/color]

then the layer is changed. what's going on here?

 

The command it is active and it is waiting for some input, so it needs to get feed first with something like the lines below:

 

(command "_.dimlinear")
(while (eq 1 (logand 1 (getvar "cmdactive"))) (command pause))

 

Then, it can be set the previous layer in your case using setvar clayer.

 

To test this, before loading your routine (from vlisp editor) select the option 'Animate' from the 'Debug' pull-down menu.

 

A minimal brute use of code, below, to test an alternative call for Dimlinear, in this case requires three pauses, first two to define the dimension length and the last one the location - in other words pass the same numbers of arguments for the command in test:

 

(defun c:mydim ()
 (if (tblsearch "layer" "dimension")
   (command "_.layer" "s"    "dimension"   ""
     "_.dimlinear"    pause      pause  pause
     "_.layer" "s"    "0"       ""
    )
    'NO-LAYER-DIMENSION
 )
)

 

HTH

Share this post


Link to post
Share on other sites
Lee Mac
The command it is active and it is waiting for some input, so it needs to get feed first with something like the lines below:

 

(command "_.dimlinear")
(while (eq 1 (logand 1 (getvar "cmdactive"))) (command pause))

 

Not quite sure how I missed that... seeing that we were only discussing logand earlier in the thread... :oops:

 

Nice one Mr Esquivel :)

Share this post


Link to post
Share on other sites
LEsq
Not quite sure how I missed that... seeing that we were only discussing logand earlier in the thread... :oops:

 

Nice one Mr Esquivel :)

 

it happens to all, been there many times... :)

Share this post


Link to post
Share on other sites
guitarguy1685
The command it is active and it is waiting for some input, so it needs to get feed first with something like the lines below:

 

(command "_.dimlinear")
(while (eq 1 (logand 1 (getvar "cmdactive"))) (command pause))

Then, it can be set the previous layer in your case using setvar clayer.

 

To test this, before loading your routine (from vlisp editor) select the option 'Animate' from the 'Debug' pull-down menu.

HTH

 

so when I inserted the 2 pauses was that the same as input? Either way the cmdactive line works great. I like it because i can use the dimlinear as a normal autocad command. I didn't want to specify 3 points becuse some times I like to press enter and use the object command. This is great. Thanks for your help guys. I've learned so much from this site.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×