Jump to content

Getint+Polar and different result


Grrr

Recommended Posts

Hi fellas,

I just wrote this:

; Attempt for dimaligned + dimcontinue
(defun C:test ( / oldosm pt1 pt2 pt3 ang dist )
(setq oldosm (getvar 'osmode))
(if
	(and
		(setq dist (getint "\nSpecify offset value"))
		(setq pt1 (getpoint "\nPoint1 "))
	)
	(while
		(setq pt2 (getpoint pt1 "\nPoint2 "))
		(setq ang (angle pt1 pt2))
		(setq pt3 (polar (mid pt1 pt2) (+ ang (/ 2 PI)) dist)) ; should be perpendicular from the midpoint between pt1 and pt2, on "dist" distance
		(setvar 'osmode 0)
		(command "_.dimaligned" pt1 pt2 pt3)
		(setq pt1 pt2)
		(setvar 'osmode oldosm)
	)
)
(princ)
)

(defun mid (p1 p2)
(mapcar '(lambda (x1 x2) (/ (+ x1 x2) 2.0)) p1 p2)
)

 

I don't have much experience with the polar function, but honestly I can't see anything wrong in there.

However after inputting value of 100 for the offset value I got this result:

OffProblem.jpg

Instead of 100 units offset, I have 59,xxx.

I can't figure out whats the problem. The pt3 should be located perpendicular from the midpoint of the pt1-pt2 line with distance dist.

Link to comment
Share on other sites

Good start and nothing is wrong with your polar function except the value that represents the 90 Degree angle in Radians.

eg:

(/ pi 2.0)

 

A few advises if you want to:

  • When you ask user for a value and that value must be real number it is better to use getdist or even getreal instead of function getint since the last fore-said function would return integer and not real number.
  • Try to avoid using System Variable osmode since you don't have error function handler to re-set them back when a user hit ESC button to end the program, so in your case you can use the option "_non" or "_none" to ignore the settings of OPSNAP.

Link to comment
Share on other sites

Thank you, Tharwat!

Finding that perpendicular vector was always a question for me, untill your reply. Now I can fix up some other codes, that I had in mind.

I did put getint on purpose, I am familiar of getdist and getreal.

I'll follow your second advice, but when I really decide to use some of my "practice" routines for a daily work I always add function error handler.

 

As for the copyrights, I don't profit any money from writing codes (they just help me for my job).

So I add to the credits the guys who helped me (usually its you and/or Lee mac) and I write myself as an author - to know that there might be something wrong in the code.

Link to comment
Share on other sites

Honestly, I went a little further with my experimetning and decided to mess up with DIMSPACE.

I changed this:

(setq dist (getint "\nSpecify offset value"))

to this:

(setq dist (distance pt1 pt2))

So I could track the result from the error handler.

Heres what I've assembled for this experiment:

; Attempt for dimaligned + dimcontinue
(defun C:test ( / *error* oldosm pt1 pt2 pt3 ang dist ss )

(defun *error* ( msg )
	(if oldosm (setvar 'osmode oldosm))
	(command-s "_.DIMSPACE" (entlast) ss "" 0)
       (if (not (member msg '("Function cancelled" "quit / exit abort")))
           (princ (strcat "\nError: " msg))
	)
       (princ)
)

(setq oldosm (getvar 'osmode))
(if
	(and
		; (setq dist (getint "\nSpecify offset value"))
		(setq pt1 (getpoint "\nPoint1 "))
	)
	(progn
		(setq ss (ssadd))
		(while
			(and
				(setq pt2 (getpoint pt1 "\nPoint2 "))	
			)
			(setq ang (angle pt1 pt2))
			(setq dist (distance pt1 pt2))
			(setq pt3 (polar (mid pt1 pt2) (+ ang (/ PI 2)) dist)) ; should be perpendicular from the midpoint between pt1 and pt2, on "dist" distance
			(setvar 'osmode 0)
			(command "_.dimaligned" "_non" pt1 "_non" pt2 "_non" pt3)
			(setq pt1 pt2)
			(setvar 'osmode oldosm)
			(setq ss (ssadd (entlast) ss))	
		)
	)
)
(princ)
)

(defun mid (p1 p2)
(mapcar '(lambda (x1 x2) (/ (+ x1 x2) 2.0)) p1 p2)
)	

Usage example:

Pick the points to be collinear, at different distances (so the created dimensions would be paralel, but obviously not collinear).

Then hit ESC, and they should become collinear to the last created.

 

I have to admit that its really interesting to write these, when you become more skilled as the time passes! :)

Link to comment
Share on other sites

I guess you did not get what I have said about the OSMODE settings since you have re-used them once again with "_non" option, so use either of these OSMODE or "_non".

 

You have used AND function two times without the need of it.

To have a correct return value when you expect a real value from dividing number on another you should feed them both reals as I have used the PI divided on two.

Link to comment
Share on other sites

I understood everything!

Just didn't wanted to remove the and functions as I constantly change the code (and to not bother rewriting them), the same goes for progn function.

But yeah, I haven't noticed that I should feed PI with reals, I'll remember that!

Link to comment
Share on other sites

For lots of 90 180 270 45 etc make a series of global values and save the code in a library lisp like acaddoc.lsp.

 

(setq p90 (/ pi 2.0))
(setq p180 pi)
(setq p45 (/ pi 4.0))
(setq p270 (* pi 1.5))

Link to comment
Share on other sites

Good advice, BIGAL!

I thought about using the DtR function:

Command: (setq p45 (/ pi 4.0))
0.785398
Command: (defun DtR (d) ( * PI (/ d 180.0)))
DTR
Command: (DtR 45)
0.785398

It seems handy for people like me who aren't used with radians.

 

Yesterday I've found an easy way to check a series of points, each returned from the polar function.

Instead of creating point object, I entmake mtext with middlecenter justification with content of the name of the point (like I quoted it in the code). Example:

(defun C:test ( / pt1 pt2 midpt keypt dist ang )
(while 
	(and
		(setq pt1 (getpoint "\nPick first point"))
		(setq pt2 (getpoint pt1 "\nPick second point"))
	)
	(progn
		(setq dist (distance pt1 pt2))
		(setq ang (angle pt1 pt2))
		(setq midpt (polar pt1 ang (/ dist 2.0)))
		(setq keypt (polar midpt (+ ang (/ pi 2.0)) dist)) ; keypt-midpt should be perpendicular to pt1-pt2
		
		; perform points check:
		(M-Text pt1 "pt1")
		(M-Text pt2 "pt2")
		(M-Text midpt "midpt")
		(M-Text keypt "keypt")
		
		; perform length check:
		(Line pt1 pt2)
		(Line midpt keypt)
	);progn
)
(princ)
);defun

; LM
(defun M-Text (pt str)
(entmakex 
	(list 
		(cons 0 "MTEXT")         
		(cons 100 "AcDbEntity")
		(cons 100 "AcDbMText")
		(cons 10 pt)
		(cons 1 str)
		(cons 71 5)
	)
)
)

(defun Line (p1 p2)
(entmakex 
	(list 
		(cons 0 "LINE")
		(cons 10 p1)
		(cons 11 p2)
	)
)
)

Polar Checking.jpg

Now I think that I won't have problems anymore using and imagining the polar function (as I struggled in my previous thread "draw a square").

This example would ease anyone, trying to "draw" more complex shapes in his code.

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