Passing variable as relative in COMMAND function

Passing variable as relative in COMMAND function

126843
Participant Participant
3,238 Views
29 Replies
Message 1 of 30

Passing variable as relative in COMMAND function

126843
Participant
Participant

Is there a way to pass a variable as relative into for example a rectang function?

 

This is part of my code and it doesnt work

 

        (setq p1 ip)
	(setq p2 ( list o_width o_height 0))
	(command "_rectang"
			
		p1
		
		@p2  ; how to pass p2 as relative? this doesnt work
		
		""
	)

How i cand 

0 Likes
3,239 Views
29 Replies
Replies (29)
Message 2 of 30

andkal
Collaborator
Collaborator

You can calculate p2 coordinates before the "rectang" command and pass p2 to that command as absolute instead of relative.:

(setq p1 ip )
(setq p2 ( list (+ (car p1) o_width) (+ (cadr p1) o_height) 0))
(command "_rectang" p1 p2 )

• www.autolisps.blogspot.com - Productivity plugins for Autocad and Autocad MEP
• Autodesk AppStore
0 Likes
Message 3 of 30

john.uhden
Mentor
Mentor

or...

 

(setq p2 (mapcar '+ p1 (list o_width o_height 0)))

 

Plus, if there happen to be running object snaps (to avoid)...

 

(command "_rectang" "_non" p1 "_non" p2)

John F. Uhden

0 Likes
Message 4 of 30

Kent1Cooper
Consultant
Consultant

Another possible approach, without the need for the p2 variable [untested]:

 

        (setq p1 ip)
	(command "_rectang"
		p1		
		(strcat "@" (rtos o_width) "," (rtos o_height) ",0")
	)
	
 

The "" in yours is extraneous, by the way -- giving the second corner of the rectangle completes the RECTANG command. 

 

Kent Cooper, AIA
Message 5 of 30

john.uhden
Mentor
Mentor
That's the cleverness we can always expect from you. Just one question...
Might you want to specify the precision? Oops, make that two questions...
might (rtos value 2 8) be not precise enough, as in what if the user is
dealing with fractions of angstroms, but the units are meters? I am
implying that in some cases it may be better to pass reals than approximate
reals. Of course if the purpose is only to specify a box for mtext, then
precision should rarely matter.

John F. Uhden

0 Likes
Message 6 of 30

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

Another possible approach, without the need for the p2 variable [untested]:

 

        (setq p1 ip)
	(command "_rectang"
		p1		
		(strcat "@" (rtos o_width) "," (rtos o_height) ",0")
	)
 ... 

Come to think of it, you don't need the p1 variable either:

(command "_rectang"
  ip
  (strcat "@" (rtos o_width) "," (rtos o_height) ",0")
)

Kent Cooper, AIA
0 Likes
Message 7 of 30

andkal
Collaborator
Collaborator

Autocad, when asking for the first coordinate (numeric value) of the second point, probably doesnt uderstand the argument f.e. "@123,256" [retrurn value of (strcat "@" (rtos o_width) "," (rtos o_height) ",0" ]
becouse it still asks for the X coordinate. Even if I separate the inside the (strcat) like so:

(command "_rectang" p1 "@" o_width o_height)


autocad doesnt accept "@" to let the user pass the width and height, but draws a rectangle with corners of the same coordinates as p1 (in one point).

I wonder if there's a way to make autocad understand "@". (command "@" ) doesnt work...


• www.autolisps.blogspot.com - Productivity plugins for Autocad and Autocad MEP
• Autodesk AppStore
0 Likes
Message 8 of 30

Ranjit_Singh
Advisor
Advisor

@andkal wrote:

Autocad, when asking for the first coordinate (numeric value) of the second point, probably doesnt uderstand the argument f.e. "@123,256" [retrurn value of (strcat "@" (rtos o_width) "," (rtos o_height) ",0" ]
becouse it still asks for the X coordinate. Even if I separate the inside the (strcat) like so:

(command "_rectang" p1 "@" o_width o_height)


autocad doesnt accept "@" to let the user pass the width and height, but draws a rectangle with corners of the same coordinates as p1 (in one point).

I wonder if there's a way to make autocad understand "@". (command "@" ) doesnt work...


Post 6 has an additional ",0" at the end. Rectangle needs a 2d point. Get rid of that 0

        (setq p1 ip)
	(command "_rectang"
		p1		
		(strcat "@" (rtos o_width) "," (rtos o_height))
	)
0 Likes
Message 9 of 30

Kent1Cooper
Consultant
Consultant

@andkal wrote:

... doesnt accept "@" to let the user pass the width and height, but draws a rectangle with corners of the same coordinates as p1 (in one point).

I wonder if there's a way to make autocad understand "@". (command "@" ) doesnt work...


It does recognize it, but "@" by itself  specifically means  at the same place as the last point, equivalent to "@0,0" [or, where XYZ points are accepted, "@0,0,0"].  Try @Ranjit_Singh's suggestion to omit the Z coordinate from the second point -- that kind of correction is what I get for not testing it.

Kent Cooper, AIA
0 Likes
Message 10 of 30

andkal
Collaborator
Collaborator

OK, it works now. I missunderstood why it didnt work


• www.autolisps.blogspot.com - Productivity plugins for Autocad and Autocad MEP
• Autodesk AppStore
0 Likes
Message 11 of 30

john.uhden
Mentor
Mentor
This is all getting stupid. What matters more, the looks of the code or
the results of the code?
p1 and p2 give you results.

John F. Uhden

0 Likes
Message 12 of 30

126843
Participant
Participant

Maybe i asked a wrong question

 

If you are using _rectang or any other functions you may input second ( or n-th) point using relatve dimensions (with@) Is there a way to code that way an autolisp without any additional questions ( PROMPT: "Do you want choose second Point or pass Relative distance?  P/R")   ?

 

It will be handy for user if he will have both input methods without anserwing questions in command line 

 

 

 

0 Likes
Message 13 of 30

126843
Participant
Participant

Maybe it can be done by writing function which recognize user input data type: point or real?

 

But how to done it?

0 Likes
Message 14 of 30

john.uhden
Mentor
Mentor

If I recall correctly, all AutoCAD commands prompt for the "next" point, so it's just a matter of how you wish to acquire it and then provide it to the command.

John F. Uhden

0 Likes
Message 15 of 30

126843
Participant
Participant

Yes but you can do it in two ways. You can pass direct coordinates (or click on screen) or pass  indirect coordinates using "@" and autocad can handle it. How they done it?

0 Likes
Message 16 of 30

Kent1Cooper
Consultant
Consultant

@126843 wrote:

.... 

It will be handy for user if he will have both input methods without anserwing questions in command line 

.... 


An ordinary pause will allow both input methods:

(command "_rectang" p1 pause .....

 

But if you're trying to incorporate a value from a variable, I can't imagine a way to have an option to use it in either absolute or relative terms without  the User answering some kind of question.

 

Or am I misunderstanding what you want to do?

Kent Cooper, AIA
0 Likes
Message 17 of 30

Kent1Cooper
Consultant
Consultant

@126843 wrote:

Maybe it can be done by writing function which recognize user input data type: point or real?

....


You can have it distinguish between those two data types:

 

Command: (initget 128) ; to accept arbitrary input, i.e. a typed number rather than a point pick in a (getpoint) function

Command: (setq test (getpoint "\nPick point or type real number: "))

Pick point or type real number: (30.9431 11.5841 0.0) ; picked a point

Command: (type test)
LIST ; the "type" that a point is

Command: (initget 128)

Command: (setq test (getpoint "\nPick point or type real number: "))

Pick point or type real number: 1.2345 ; typed a number:
"1.2345"

Command: (type test)
STR ; the "type" of the User input

 

Then you would have to convert that string to a number with (atof).

 

I tried using (getreal ) instead of (getpoint ), to get around the String-type return needing conversion, but unfortunately, (initget 128) for arbitrary input accepts only arbitrary typed  input, not a point pick.

Kent Cooper, AIA
0 Likes
Message 18 of 30

Ranjit_Singh
Advisor
Advisor

@126843 wrote:

Yes but you can do it in two ways. You can pass direct coordinates (or click on screen) or pass  indirect coordinates using "@" and autocad can handle it. How they done it?


How they done it? I am not sure. But try something like this. it should allow picking on screen or passing relative co-ordinates.

(defun c:somefunc  (/ p1 p2)
 (setq p2 (getcorner "\n Specify second point (you can use relative co-ords here or pick on screen): "
                    (setq p1 (getpoint "\nSpecify first point: "))))
 (command "._rectang" p1 p2))

Relative_coordinates_always_allowed.gif

 

0 Likes
Message 19 of 30

ActivistInvestor
Mentor
Mentor

@126843 wrote:

Maybe i asked a wrong question

 

If you are using _rectang or any other functions you may input second ( or n-th) point using relatve dimensions (with@) Is there a way to code that way an autolisp without any additional questions ( PROMPT: "Do you want choose second Point or pass Relative distance?  P/R")   ?

 

It will be handy for user if he will have both input methods without anserwing questions in command line 

 

 

 


@I don't see the point to that, because the user already has the ability to use relative or absolute coordinate entry at any prompt, including those issued by your calls to (getpoint). The "@" means relative to the last point entered (which is stored in the LASTPOINT system variable). So, they can type @ in front of either a Cartesian or polar coordinate entry to enter a relative cartesian or polar coordinate. 

 

Deviating from the standards that AutoCAD uses for data entry is not a best-practice.

0 Likes
Message 20 of 30

john.uhden
Mentor
Mentor

@ActivistInvestor wrote: "the ability to use relative or absolute coordinate entry at any prompt, including those issued by your calls to (getpoint). The "@" means relative to the last point entered (which is stored in the LASTPOINT system variable)."

 

Just a slight clarification:

The point stored in the LASTPOINT sysvar is one entered into an AutoCAD command,not necessarily one acquired by an AutoLisp function such as getpoint.

For example, if you (setq p1 (getpoint)), (getvar "LASTPOINT") will probably not return the value of p1.

BUT, LASTPOINT is not read-only, so you could (setvar "LASTPOINT" p1)

John F. Uhden

0 Likes