Linetype program not setting linetype to current

Linetype program not setting linetype to current

murmanator
Advocate Advocate
2,185 Views
20 Replies
Message 1 of 21

Linetype program not setting linetype to current

murmanator
Advocate
Advocate

Hi, I made this program that sets layer, color and linetype in order to draw a line. The linetype part is supposed to search for the type, load it if its not in the drawing, and set it to current. Everything seems to be working except for setting the linetype to current. It does load it just dosent set it. If I run the program with the linetype loaded it works fine. What am I missing? I have layer programs that are much like this and work like they should.

 

(DEFUN C:ITREE ()
	(GV)
	(command "-LAYER" "set" "RUNS" "" "" "")
	(command "-color" "YELLOW")
		(SETVAR 'CMDECHO 0)
		(if (= (assoc 0 (tblsearch "Ltype" "I_TREES")) nil)
			(vl-cmdf "-linetype" "l" "I_TREES" "" "" )
			(vl-cmdf "-linetype" "s" "I_TREES" "")
		)
	(command "_.pline" pause)
		(while (> (getvar "CMDACTIVE") 0) (command pause))
	(command "-linetype" "set" "Bylayer" "")
	(SV)
    (princ)
)

  

0 Likes
Accepted solutions (1)
2,186 Views
20 Replies
Replies (20)
Message 2 of 21

cdmiske
Enthusiast
Enthusiast

I use this to set my line type to the layer I need in my routines when I need things to be drawn on a certain layer.

 

(setvar "clayer" "[layer name]")

0 Likes
Message 3 of 21

murmanator
Advocate
Advocate

The layer part is not the problem. Its the linetype part. I expect its some aspect of the IF function combining the loading of the linetype AND setting it to the current linetype.

0 Likes
Message 4 of 21

cdmiske
Enthusiast
Enthusiast

Ah! I miss read what you are looking for. I have used this to reset my linetype on a routine I have for converting layers.

 

(setvar 'CELTYPE "[Linetype]") 

0 Likes
Message 5 of 21

Kent1Cooper
Consultant
Consultant

@murmanator wrote:

....

....
		(if (= (assoc 0 (tblsearch "Ltype" "I_TREES")) nil)
			(vl-cmdf "-linetype" "l" "I_TREES" "" "" )
			(vl-cmdf "-linetype" "s" "I_TREES" "")
		)
....

  


If you want it to do both of those things as a 'then' argument to the (if) function, you need to make them into one argument, which you do by "wrapping" them in a (progn) function:

....

(if (= (assoc 0 (tblsearch "Ltype" "I_TREES")) nil)

  (progn
    (vl-cmdf "-linetype" "l" "I_TREES" "" "" )
    (vl-cmdf "-linetype" "s" "I_TREES" "")

  ); progn
)

....

As you have it, the setting of that linetype current is the 'else' argument, to be done if the test function returns nil.

 

EDIT:  Or, do I assume you want that linetype set current whether or not  it had to be loaded first?  In that case, pull that out of the (if) function:

....

(if (= (assoc 0 (tblsearch "Ltype" "I_TREES")) nil)

  (vl-cmdf "-linetype" "l" "I_TREES" "" "" ); then [no else argument]

); if
(vl-cmdf "-linetype" "s" "I_TREES" "")

....

 

FURTHER EDIT:  Shouldn't you also set the Color back to ByLayer, as you do the Linetype?  Or does (SV) do that?

Kent Cooper, AIA
0 Likes
Message 6 of 21

ВeekeeCZ
Consultant
Consultant

Yes, the IF is the issue. The "SET" part should be OUTSIDE of the IF.

 

(if (not (tblsearch "Ltype" "I_TREES"))
   (vl-cmdf "-linetype" "l" "I_TREES" "" ""))

(vl-cmdf "-linetype" "s" "I_TREES" "")

 

0 Likes
Message 7 of 21

CodeDing
Advisor
Advisor
Accepted solution

@murmanator ,

 

Lots of good points brought up so far.

Here's my take of your updated stuff.

Maybe this will help? (untested)

(defun C:ITREE ()
  (GV)
  (command "-LAYER" "set" "RUNS" "" "" "")
  (command "-COLOR" "YELLOW")
  (if (not (tblsearch "LTYPE" "I_TREES"))
    (command "-LINETYPE" "l" "I_TREES" "" "" )
  );if
  (command "-LINETYPE" "s" "I_TREES" "")
  (command-s "_.PLINE")
  (command "-LINETYPE" "set" "Bylayer" "")
  (command "-COLOR" "ByLayer")
  (SV)
  (princ)
);defun

Best,

~DD

0 Likes
Message 8 of 21

Kent1Cooper
Consultant
Consultant

Here's another trick you can try:

If you put your linetype definition(s) into ACAD.lin  [or ACADISO.lin if that's what you use], then you can assign one to something using CHPROP, or assign it to a Layer in a command-line LAYER command, and it won't matter whether it's already loaded in the drawing -- AutoCAD will find it.  You won't  need to load it, nor therefore to check whether it's loaded or not.

 

If I assume your (GV) and (SV) functions are for Getting and Setting Variables, presumably System Variables, then if you use CHPROP for more than just the linetype, you won't change any of those, and therefore won't need to reset them.  Then all you need is this:

 

(defun C:ITREE ()
  (command-s "_.PLINE")
  (command "_.chprop" "_last" "" "_layer" "RUNS" "_color" 2 "_ltype" "I_TREES" "")
  (princ)
);defun

 

But additionally, I wonder why you don't just have a Layer for this kind of linework, with the color and linetype assigned to it, so you can just set that Layer current, and leave everything ByLayer, rather than using property overrides on things.

Kent Cooper, AIA
Message 9 of 21

CodeDing
Advisor
Advisor

@Kent1Cooper ,

 

I will never underestimate your ability to utilize command calls so effectively 😅

0 Likes
Message 10 of 21

murmanator
Advocate
Advocate

Thanks everyone for all the great stuff. I tried all the suggestions, the one I got to work was this:

 

(defun C:ITREE ()
(GV)
(command "-LAYER" "set" "RUNS" "" "" "")
(command "-COLOR" "YELLOW")
(if (not (tblsearch "LTYPE" "I_TREES"))
(command "-LINETYPE" "l" "I_TREES" "" "" )
);if
(command "-LINETYPE" "s" "I_TREES" "")
(command-s "_.PLINE")
(while (> (getvar "CMDACTIVE") 0) (command pause))
(command "-LINETYPE" "set" "Bylayer" "")
(SV)
(princ)
);defun

 

I had to tweak the end of it to get the variables back but its working good now. Thank you CodeDing and everyone else that replied!

0 Likes
Message 11 of 21

CodeDing
Advisor
Advisor

@murmanator ,

 

If you haven't tried @Kent1Cooper 's code, I would recommend giving that a go.

If his does not work for you, can you elaborate why?

Your call either way. Kent's method just utilizes the built-in tools very effectively.

 

Best,

~DD

0 Likes
Message 12 of 21

Kent1Cooper
Consultant
Consultant

@murmanator wrote:

....

(command "-LAYER" "set" "RUNS" "" "" "")
....

(command-s "_.PLINE")
(while ....


Just quickly....

 

Two too many Enters at the end of that Layer command.  The single one will end the command -- the other two will just recall the command and then get right back out of it.

 

If you use (command-s) for the PLINE command, you don't need  that  (while ....  function.

 

And I still think you should also be resetting the Color, but if that's covered by the (SV) function, can't the linetype setting be, also, so you don't need to do it separately?  And maybe reset the Layer, too?

Kent Cooper, AIA
0 Likes
Message 13 of 21

murmanator
Advocate
Advocate

CodeDing, all the suggestions to move the set function outside of the IF I could not get to work (except yours). I would get "unknown command". Probably an issue on my end. The one with the chprop didnt set the variables, it just drew the line. Im always excited to have Kent respond to my post, he is a genius. 

0 Likes
Message 14 of 21

murmanator
Advocate
Advocate

Thanks Kent. You're right the GV/SV gets and restores the variables, for some reason it didnt work to restore the linetype which is why I threw in the specific command to do that.

0 Likes
Message 15 of 21

Kent1Cooper
Consultant
Consultant

@murmanator wrote:

.... The one with the chprop didnt set the variables, it just drew the line. .... 


Yes, that was the point -- don't change the System Variable settings, just impose the properties on the Polyline.  But does "just drew the [Poly]line" mean that it didn't  then give it those properties?  If not, were there any messages?

 

If it changed the Layer and Color, but not the Linetype, it suggests that you didn't notice the "If" at the beginning of the Message.  Linetype definitions must be in AutoCAD's linetype file  for the CHPROP approach to find them even if they're not loaded yet in the drawing.

 

Of course that approach has the "drawback" [if you consider it one] that while you're drawing it, you're under the current Layer and Color and Linetype settings, so it may not look like what you'll end up with in the process, but only afterwards.

Kent Cooper, AIA
0 Likes
Message 16 of 21

ВeekeeCZ
Consultant
Consultant

@CodeDing wrote:

@murmanator ,

 

If you haven't tried @Kent1Cooper 's code, I would recommend giving that a go.

If his does not work for you, can you elaborate why?

Your call either way. Kent's method just utilizes the built-in tools very effectively.

 

Best,

~DD



If I can add my 2 cents... don't like Kent's suggestion for 2 reasons

 

- one is the obvious one - it does not make a preview of actual properties - as Kent noted in his last reply. 

- the second is not that much obvious:

command-s and while 'cmdactive behave differently when comes to ESC breaking. Whether the latter causes simple error and the routine stops from processing, the command-s still cancels the active command but the program continues to process a following line - so it still changes properties of LAST object, whatever it was. 

 

So I would suggest to the OP to remove "-s" from his final code.


Message 17 of 21

CodeDing
Advisor
Advisor

@ВeekeeCZ wrote:
...the command-s still cancels the active command but the program continues to process a following line...


Wow, had no idea. definitely good thing to know!

0 Likes
Message 18 of 21

murmanator
Advocate
Advocate

Kent, when I run the chprop program I get this in the command line:

 

_.PLINE
Specify start point: _.chprop
Invalid point.
; error: Function cancelled
Specify start point:
Current line-width is 0'-0"

 

I draw the line but then the properties are not changed. I did see the IF part and do have my linetypes in the acad.lin file which is in a support folder. Thanks

0 Likes
Message 19 of 21

Kent1Cooper
Consultant
Consultant

@murmanator wrote:

.... when I run the chprop program I get this in the command line:

 

_.PLINE
Specify start point: _.chprop
Invalid point.....


I don't know what to suggest -- it works for me.  It lets me draw a Polyline, of any length, then changes those properties correctly.  I changed the Layer and linetype names to things I have, but nothing else.

 

Something about your setup must not be honoring the (command-s) let-you-finish operation, and is jumping right to the next thing, but I have no idea what that could be.

Kent Cooper, AIA
0 Likes
Message 20 of 21

murmanator
Advocate
Advocate

Kent, Im not sure either but I am running an older version of CAD in 2013. I know some LISP things change with versions. I did try it without the -s after the command and it did the same thing.

0 Likes