Jump to content

IF Statement question


ILoveMadoka

Recommended Posts

I was under the impression that if you had an IF statement, the code following was conditional

 

(if (setq A1 (ssget "_X" '((0 . "DIMENSION"))))
(command "change" A1 "" "P" "La" "Dim" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  ""))

 

In this instance, shouldn't it check to see if there are any DIMENSIONs and if so proceed, if not exit?

 

This is part of a routine I am working on and it works if there are dimensions found but if no dimensions are present I get an error.


 

Unknown command "P".  Press F1 for help.
Unknown command "LA".  Press F1 for help.

*Invalid selection*
Expects a point or Last
*Cancel*
Select continued dimension: *Cancel*

 

Can someone please clarify.

Definitely willing to admit that my understanding is limited...

 

My goal is if not found, exit.

Link to comment
Share on other sites

(if (setq A1 (ssget "_X" '((0 . "DIMENSION"))));;; if A1 exists...
 (command "change" A1 "" "P" "La" "Dim" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  "");;; true condition
 (vl-exit-with-error (alert "No dimension!"));;; <-- if no dimension, false condition
)

 

In the if function, if the condition is true everything inside the first progn is valid, otherwise the second (if there is only one line of programming the progn instruction is not needed). You did not specify the false condition, so you got the error message.

Edited by confutatis
Link to comment
Share on other sites

You dont need to have a false statement for an if statement to work in BricsCAD. This might be an SolidWorks issue. both of these work for me with out errors.

 

  (if (setq A1 (ssget "_X" '((0 . "DIMENSION"))))
    (command "change" A1 "" "P" "La" "Dim" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  "")
  )
  
(if (setq A1 (ssget "_X" '((0 . "DIMENSION"))))
(command "change" A1 "" "P" "La" "Dim" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  ""))

 

This also is a good write up.

Quote

(if (only one predicate)  ;must return T, even if it is a double negative

(then do something)      ;if the predicate is true, then do this

                                          ;prescribed something

(else do this)                  ;this is optional, if there is an else, then

                                         ;this is what happens

                                         ;if the predicate is not true

)

https://www.afralisp.net/autolisp/tutorials/cond-vs-if.php

Link to comment
Share on other sites

1 hour ago, ILoveMadoka said:

 




(if (setq A1 (ssget "_X" '((0 . "DIMENSION"))))
(command "change" A1 "" "P" "La" "Dim" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  ""))

 

 

That should work as it stands now. The error appears ONLY if there dimensions found, either on a locked layer [ not seen on screen probably off/frozen ] 

or the Dimension object(s) are on another layout other than the current tab not accessible by command call

 

Try changing the ssget filter to something like this

(setq A1 (ssget "_X" (list '(0 . "DIMENSION")(Cons 410 (getvar 'ctab)))))

 

Edited by pBe
  • Like 1
  • Agree 1
Link to comment
Share on other sites

As a matter of mental cleanliness, I always prefer to put both conditions, regardless. I don't know BricsCAD and I used to get an error message anyway, so I prefer the confirmation message as if the condition is false.

Link to comment
Share on other sites

Follow up questions...

 

(if (setq A1 (ssget "_X" '((0 . "DIMENSION"))));;; if A1 exists...
 (command "change" A1 "" "P" "La" "Dim" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  "");;; true condition
 (vl-exit-with-error (alert "No dimension!")));;; <-- if no dimension, false condition

 

What if I simply want it to move on to the next step vs returning an error message?

How would this code appear?

 

(if (setq A1 (ssget "_X" (list '(0 . "DIMENSION")(Cons 410 (getvar 'ctab)))))
(command "change" A1 "" "P" "La" "Dim" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  "")
)

 

Can this be applied to all dimensions within the drawing, on all tabs vs having to tab through all tabs repeating the steps?

 

Thanks to all who replied.

I appreciate it greatly..

 

Link to comment
Share on other sites

11 hours ago, ILoveMadoka said:

Follow up questions...

What if I simply want it to move on to the next step vs returning an error message?

How would this code appear?

 

like I said the false statement is optional. so if you want to move on without any prompts don't put a false statement in. it would appear like this.

 

(if (setq A1 (ssget "_X" (list '(0 . "DIMENSION")(Cons 410 (getvar 'ctab)))))
   (command "change" A1 "" "P" "La" "Dim" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  "")
 )

 

11 hours ago, ILoveMadoka said:

Can this be applied to all dimensions within the drawing, on all tabs vs having to tab through all tabs repeating the steps?

 

look into entmod its a visual lisp

Edited by mhupp
fixed code to only work with current tab
Link to comment
Share on other sites

I don't want to say something stupid, but the code that appears in the last post is identical to the initial one, so it should come out the error message that appears in the post n°1.

Link to comment
Share on other sites

Full disclosure (Novice Level)

 

here is what I have so far.

 

(defun c:d2d ()
(if (setq A1 (ssget "_X" '((0 . "DIMENSION"))))
 (command "change" A1 "" "P" "La" "Dim" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  "")
 (vl-exit-with-error (alert "No dimension!")))
(prin1 "A1")
(if (setq A2 (ssget "_X" '((0 . "TEXT,MTEXT"))))
 (command "change" A2 "" "P" "La" "TEXT" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  "")
 (vl-exit-with-error (alert "No text!")))
(prin1 "A2")
(if (setq A3 (ssget "_X" '((0 . "INSERT,ATTDEF"))))
 (command "change" A3 "" "P" "La" "0" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  "")
 (vl-exit-with-error (alert "No blocks!")))
(prin1 "A3")
(if (setq A4 (ssget "_X" '((0 . "LEADER,MULTILEADER"))))
 (command "change" A4 "" "P" "La" "DIM" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  "") 
 (vl-exit-with-error (alert "No multileaders!")))
(prin1 "A4");;-------------------------------------------good to here
(if (setq A5 (ssget "_X" (list (cons 8 "SLD-0"))))
(command "change" A5 "" "P" "La" "0" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  "")
 (vl-exit-with-error (alert "None Found!")))
(prin1 "A5")
(if (setq A7 (ssget "_X" '((-4 . "<OR") (62 . 7) (62 . 8) (-4 . "OR>"))) )
(command "change" A7"" "P" "co" "bylayer"  "lw" "bylayer" "lt" "bylayer"  ""))
(prin1 "A7")
(princ))

 

Given a drawing where each statement has entities that fit the criteria, the code works perfectly until it gets to the A5 section

(the prin1 statements were added for checking purposes only). Kiddie level error checking.

 

When I run the routine, all the steps complete correctly but this is the response.

note: A5 & A7 complete. They "work" but I get the following errors.

Again, there are entities meeting all the criteria in this drawing.

 

Command: d2d
"A1""A2""A3""A4"Unknown command "P".  Press F1 for help.
Unknown command "LA".  Press F1 for help.
Unknown command "0".  Press F1 for help.
Unknown command "CO".  Press F1 for help.
Unknown command "BYLAYER".  Press F1 for help.
Unknown command "LW".  Press F1 for help.
Unknown command "BYLAYER".  Press F1 for help.
Unknown command "LT".  Press F1 for help.
Unknown command "BYLAYER".  Press F1 for help.
Unknown command "D2D".  Press F1 for help.
"A5"Unknown command "P".  Press F1 for help.
Unknown command "CO".  Press F1 for help.
Unknown command "BYLAYER".  Press F1 for help.
Unknown command "LW".  Press F1 for help.
Unknown command "BYLAYER".  Press F1 for help.
Unknown command "LT".  Press F1 for help.
Unknown command "BYLAYER".  Press F1 for help.
Unknown command "D2D".  Press F1 for help.
"A7"

Command:

 

 

 

Is it obvious to those much smarter than I what the problem is?

 

The BYLAYER command will do much of this by itself.

I'm trying to learn here too..

 

 

 

Link to comment
Share on other sites

5 hours ago, ILoveMadoka said:

Given a drawing where each statement has entities that fit the criteria, the code works perfectly until it gets to the A5 section

 

the error is self generated with vl-exit-with-error So it would exit the lisp at any point if any of those if statements were false.  taken it out and it will just display the false statement. double check that their is something on the SLD-0 layer. also changed everything to prompt instead of alert because you have to click OK for it to continue. added the VLA commands so even if something is on another tab because of the ssget "X" it will still change their proprieties.

 

(defun c:d2d (/ A1 A2 A3 A4 A5 A7)
  (vl-load-com)
  (if (setq A1 (ssget "_X" '((0 . "DIMENSION"))))
    (foreach ent (mapcar 'cadr (ssnamex A1)) ;This will change everything in A1 selection even if its on another tab.
      (vla-put-layer (vlax-ename->vla-object ent) "Dim")
      (vla-put-color (vlax-ename->vla-object ent) "256")
      (vla-put-lineweight (vlax-ename->vla-object ent) "-1")
      (vla-put-linetype (vlax-ename->vla-object ent) "ByLayer")
    )
    (prompt "\nNo dimension!")
  )
  (prompt "\nA1")
  (if (setq A2 (ssget "_X" '((0 . "TEXT,MTEXT"))))
    (foreach ent (mapcar 'cadr (ssnamex A2)) 
      (vla-put-layer (vlax-ename->vla-object ent) "TEXT")
      (vla-put-color (vlax-ename->vla-object ent) "256")
      (vla-put-lineweight (vlax-ename->vla-object ent) "-1")
      (vla-put-linetype (vlax-ename->vla-object ent) "ByLayer")
    )
    (prompt "\nNo text!")
  )
  (prompt "\nA2")
  (if (setq A3 (ssget "_X" '((0 . "INSERT,ATTDEF"))))
    (foreach ent (mapcar 'cadr (ssnamex A3)) 
      (vla-put-layer (vlax-ename->vla-object ent) "0")
      (vla-put-color (vlax-ename->vla-object ent) "256")
      (vla-put-lineweight (vlax-ename->vla-object ent) "-1")
      (vla-put-linetype (vlax-ename->vla-object ent) "ByLayer")
    )
    (prompt "\nNo blocks!")
  )
  (prompt "\nA3")
  (if (setq A4 (ssget "_X" '((0 . "LEADER,MULTILEADER"))))
    (foreach ent (mapcar 'cadr (ssnamex A4)) 
      (vla-put-layer (vlax-ename->vla-object ent) "Dim")
      (vla-put-color (vlax-ename->vla-object ent) "256")
      (vla-put-lineweight (vlax-ename->vla-object ent) "-1")
      (vla-put-linetype (vlax-ename->vla-object ent) "ByLayer")
    )
    (prompt "\nNo multileaders!")
  )
  (prompt "\nA4")
  (if (setq A5 (ssget "_X" '((8 . "SLD-0"))))
    (foreach ent (mapcar 'cadr (ssnamex A5)) 
      (vla-put-layer (vlax-ename->vla-object ent) "0")
      (vla-put-color (vlax-ename->vla-object ent) "256")
      (vla-put-lineweight (vlax-ename->vla-object ent) "-1")
      (vla-put-linetype (vlax-ename->vla-object ent) "ByLayer")
    )
    (prompt "\nNone Found!")
  )
  (prompt "\nA5")
  (if (setq A7 (ssget "_X" '((-4 . "<OR") (62 . 7) (62 . 8) (-4 . "OR>"))))
    (foreach ent (mapcar 'cadr (ssnamex A7)) 
      (vla-put-color (vlax-ename->vla-object ent) "256")
      (vla-put-lineweight (vlax-ename->vla-object ent) "-1")
      (vla-put-linetype (vlax-ename->vla-object ent) "ByLayer")
    )
    (prompt "\nNone Found!")
  )
  (prompt "\nA7")
  (princ)
)

 

 

 

Edited by mhupp
Link to comment
Share on other sites

 

Thank you...

 

I purposely placed several objects on the SLD-0 layer just to ensure the routine would work.

 

Link to comment
Share on other sites

Next question (should I continue in this thread or start a new one?)

 

  (if (setq Ax (ssget "_X" '((-4 . "<OR") (0 . DIMENSION)(3 . SLD*)(-4 . "OR>"))))
    (foreach ent (mapcar 'cadr (ssnamex Ax)) 
   

 

Is there an option for changing the DIMSTYLE?

 

I didn't see an obvious choice in the list of

(vla-put

options

 

maybe

(vl-cmdf 

 

 

Thank you all so much for your help. I have learned quite a bit in the last couple days!!

Edited by ILoveMadoka
Link to comment
Share on other sites

thanks but I already have routines to do that one thing.

That is not what I'm asking

 

 

This changes the LINETYPE
 (vla-put-linetype (vlax-ename->vla-object ent) "Phantom")
This changes the COLOR
  (vla-put-color (vlax-ename->vla-object ent) "256")
This changes the LINEWEIGHT
(vla-put-lineweight (vlax-ename->vla-object ent) "-1")
This changes the TEXT WIDTH
 (vla-put-Width (vlax-ename->vla-object ent) "0.9")
This changes the FONT
 (vla-put-stylename (vlax-ename->vla-object ent) "Simplex")

 

Is there a VLisp command for the DIMSTYLE

 

Is this what I need?

 

VLA-PUT-ACTIVEDIMSTYLE

 

Link to comment
Share on other sites

(vla-put-Activedimstyle (vla-get-activedocument (vlax-get-acad-object)) (vla-item (vla-get-dimstyles (vla-get-activedocument (vlax-get-acad-object))) "Name_dimstyle"))

 

 

Edited by confutatis
Link to comment
Share on other sites

Please forgive my lack of understanding and application but should it be like this?

I'm obviously doing something wrong here (since I can't make it work)

 

  (if (setq A1 (ssget "_X" '((0 . "DIMENSION"))))
    (foreach ent (mapcar 'cadr (ssnamex A1)) ;This will change everything in A1 selection even if its on another tab.
      (vla-put-layer (vlax-ename->vla-object ent) "Dim")
      (vla-put-color (vlax-ename->vla-object ent) "256")
      (vla-put-lineweight (vlax-ename->vla-object ent) "-1")
      (vla-put-linetype (vlax-ename->vla-object ent) "ByLayer")
         (vla-put-Activedimstyle (vla-get-activedocument 
                                                 (vlax-get-acad-object))
                                                   (vla-item (vla-get-dimstyles 
                                                     (vla-get-activedocument
                                                       (vlax-get-acad-object))) "MyDimStyle"))
    )
    (prompt "\nNo dimension!")
  )

 

Once again thank all for your help and patience...

 

Link to comment
Share on other sites

Sorry I was under a dead line at work. Someone please correct me if I am wrong but vla-put-Activedimstyle only changes the active dimension style set in the drawing. Tried to modify what i posted to work but kept doing weird stuff. but like my buddy confutatis says "Eh, I know, but sometimes laziness..." So i just went with the vl-cmdf route.

 

  (if (setq A1 (ssget "_X" '((0 . "DIMENSION"))))
    (progn ;added because multiple lines of code foreach and the two dimstyle commands
      (vl-cmdf "-DIMSTYLE" "RESTORE" "MyDimStyle") ;change to the style you want, only need to do this once outside foreach
      (vl-cmdf "-DIMSTYLE" "APPLY" A1 "") ; this can be applyed to everything at once so its outside the foreach for speed.
      (foreach ent (mapcar 'cadr (ssnamex A1))  ;This will change everything in A1 selection even if its on another tab.
        (vla-put-layer (vlax-ename->vla-object ent) "Dim") ;this will error if Dim layer isn't created
        (vla-put-color (vlax-ename->vla-object ent) "256")
        (vla-put-lineweight (vlax-ename->vla-object ent) "-1")
        (vla-put-linetype (vlax-ename->vla-object ent) "ByLayer")
      )
    )
    (prompt "\nNo dimension!")
  )

 

This is a learning experience for me as well. thought you could only do matchprop to update dimstyles.

Edited by mhupp
  • Funny 1
Link to comment
Share on other sites

I pirated some code from ronjonp

 

https://www.cadtutor.net/forum/topic/63558-change-all-existing-dimstyles-to-standard-with-a-lisp

 

and came up with this:

 

  (if (setq A1 (ssget "_X" '((0 . "DIMENSION"))))
    (foreach ent (mapcar 'cadr (ssnamex A1))
      (vla-put-layer (vlax-ename->vla-object ent) "Dim")
      (vla-put-color (vlax-ename->vla-object ent) "256")
      (vla-put-lineweight (vlax-ename->vla-object ent) "-1")
      (vla-put-linetype (vlax-ename->vla-object ent) "ByLayer")
         (vla-put-Activedimstyle (vla-get-activedocument
                                                 (vlax-get-acad-object))
                                                   (vla-item (vla-get-dimstyles
                                                     (vla-get-activedocument
                                                       (vlax-get-acad-object))) "MyDimStyle"))
    )
    (prompt "\nNo dimension!")
  )

 

This other code

 

(if (setq A1 (ssget "_X" '((0 . "DIMENSION"))))
    (progn ;added because multiple lines of code foreach and the two dimstyle commands
      (vl-cmdf "-DIMSTYLE" "RESTORE" "MyDimStyle") ;change to the style you want, only need to do this once outside foreach
      (vl-cmdf "-DIMSTYLE" "APPLY" A1 "") ; this can be applyed to everything at once so its outside the foreach for speed.
      (foreach ent (mapcar 'cadr (ssnamex A1))  ;This will change everything in A1 selection even if its on another tab.
        (vla-put-layer (vlax-ename->vla-object ent) "Dim") ;this will error if Dim layer isn't created
        (vla-put-color (vlax-ename->vla-object ent) "256")
        (vla-put-lineweight (vlax-ename->vla-object ent) "-1")
        (vla-put-linetype (vlax-ename->vla-object ent) "ByLayer")
      )
    )
    (prompt "\nNo dimension!")
  )

 

 

does the same thing but is one method "better" than the other?

 

 

Thanks so much to all who have helped me in all this!!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...