Jump to content
chiimayred

Extracting the coordinates from a rectangle in modelspace

Recommended Posts

chiimayred

Hey guys,

 

I'm trying to get the four corners of a rectangle in modelspace set to variables and I'm having a hard time with it.

 

I'm able to get the four corners into a list, but I'm having a hard time setting each coordinate to a variable.

 

The issue I'm having at this point is that the second variable duplicates the coordinate values. The first variable

 

Here's what I got so far...

 

(defun c:test (/ ptlist x y)
(setq ptlist (massoc 10 (entget(car(entsel)))))


 (defun c:massoc (key alist / x nlist)
(foreach x alist
(if (eq key (car x))
(setq nlist (cons (cdr x) nlist))
)
)
(reverse nlist)
) ;end
 
(princ ptlist)
 (setq x (car ptlist))
 (setq y (cadr ptlist))
 (princ)
 (princ x)
 (Princ)
 (princ y)
 )

 

I got part of this code from here

 

When I draw in a rectangle in modelspace and I use this code, this is what I'm getting.

 

Command: TEST
Select object: ((18.2488 -11.0958) (23.6419 -11.0958) (23.6419 -5.32136) (18.2488 -5.32136))(18.2488 -11.0958)(23.6419 -11.0958)(23.6419 -11.0958)

 

So it builds the list and sets the first variable right, but it duplicates the second.

 

Thanks in advance!

 

e: I haven't added in error trapping yet, just trying to figure this out first.

Edited by chiimayred
updated code, bit at the end

Share this post


Link to post
Share on other sites
Lee Mac

The variable is not duplicated - the function is evaluating the (princ y) expression to print the value of the variable 'y' to the command-line, and is then returning the value returned by the last evaluated expression, which is the (princ y) expression.

 

Simply add (princ) to the end of the code to ensure that the last returned value is a null symbol, and hence avoid the duplicate output.

Share this post


Link to post
Share on other sites
chiimayred

Wow, I feel like such a dunce!

 

Thanks for your reply, this is my first time really working with lists.

Share this post


Link to post
Share on other sites
Lee Mac

No worries! :)

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...