Line Joining / Polyline

Line Joining / Polyline

Anonymous
Not applicable
1,090 Views
5 Replies
Message 1 of 6

Line Joining / Polyline

Anonymous
Not applicable

Hi members, I am a newbee to lisp here.

I am working on a lisp to draw a 2D S.E. Head by inputting several parameters. Currently I am facing problems when trying to join my outer lines to form an enclosed poly-line. Tried the ".Join" command but it will also join the lines at other portion of my drawing. (P.S. I wanted to join the outer lines of the drawn head only.) 

 

Kindly see my codes as below:

 

(defun c:SE ()

(setq IP (getpoint "\nSpecify Insertion Point : "))
(setq ID (getdist "\nInner Diameter : "))
(setq SF (getdist "\nStraight Flange : "))
(setq thk (getdist "\nThickness : "))

(setq CR (* 0.9045 ID)) ;get crown radius
(setq KR (* 0.1727 ID)) ;get knuckle radius

(setq Z (- (/ ID 2) KR))
(setq H (- CR KR))
(setq L (* Z (/ CR H)))
(setq A1 (sqrt (- (expt CR 2) (expt L 2))))
(setq A2 (sqrt (- (expt H 2) (expt Z 2))))
(setq y (- A1 A2))

(setq P10 (polar IP (* PI 1) (/ ID 2))) ;OK (ID)
(setq P11 (polar IP (angle P10 IP) (/ ID 2))) ;OK (ID)

(setq P20 (polar IP (+ (/ PI 2) (atan (/ L y))) (sqrt (+ (expt L 2) (expt y 2))))) ;OK
(setq P21 (polar IP (- (/ PI 2) (atan (/ L y))) (sqrt (+ (expt L 2) (expt y 2))))) ;OK

(setq P00 (polar P10 (* PI -0.5) SF))
(setq P01 (polar P11 (* PI -0.5) SF))


(command "Line" P00 P10 "")
(command "Offset" thk P00 P10 "")

(command "Line" P11 P01 "")
(command "Offset" thk P11 P01 "")

(command "Arc" "_Non" P11 "_E" P21 "_R" KR)
(command "Offset" thk P11 P21 "")

(command "Arc" "_Non" P21 "_E" P20 "_R" CR)
(command "Offset" thk P21 P20 "")

(command "Arc" "_Non" P20 "_E" P10 "_R" KR)
(command "Offset" thk P20 P10 "")

(setq P101 (polar P10 (* PI 1) thk))
(setq P111 (polar p11 (angle P10 IP) thk))
(command "Line" P111 P101 "") ;this is tangent line
(command "Offset" SF P111 P101 "") ;this is weld line

 

(initcommandversion)
(command "_.Join" "_All" "")

(princ)
)

 

I believe there is more elegant way to write the coding. I will be very grateful if there is anyone willing to point out and guide me for this!

 

Thank you very much! 

0 Likes
Accepted solutions (1)
1,091 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
Accepted solution

Test your precision tools!
I recommend that you just merge only the created objects, "_.Join" "_All" will merge everything and I don't know if this is interesting!

 

(defun c:SE (/ lv vv ip id sf thk cr kr z h l a1 a2 y p10 p11 p20 p21 p00 p01 )

	(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
			(princ (strcat "\nError: " msg))
		)
		(mapcar 'setvar lv vv)
		(setq *error* nil)
	  (princ)
	) 
;;------------------------------------------------------------------------------;;

	(setq lv '("CMDECHO" "OSMODE"))
	(setq vv (mapcar 'getvar lv))  
	(mapcar 'setvar lv '( 0 0  ))
	(setq IP (getpoint "\nSpecify Insertion Point : "))
	(setq ID (getdist "\nInner Diameter : "))
	(setq SF (getdist "\nStraight Flange : "))
	(setq thk (getdist "\nThickness : "))
	(setq CR (* 0.9045 ID))
	(setq KR (* 0.1727 ID))
	(setq Z (- (/ ID 2) KR))
	(setq H (- CR KR))
	(setq L (* Z (/ CR H)))
	(setq A1 (sqrt (- (expt CR 2) (expt L 2))))
	(setq A2 (sqrt (- (expt H 2) (expt Z 2))))
	(setq y (- A1 A2))
	(setq P10 (polar IP (* PI 1) (/ ID 2))) ;OK (ID)
	(setq P11 (polar IP (angle P10 IP) (/ ID 2))) ;OK (ID)
	(setq P20 (polar IP (+ (/ PI 2) (atan (/ L y))) (sqrt (+ (expt L 2) (expt y 2))))) ;OK
	(setq P21 (polar IP (- (/ PI 2) (atan (/ L y))) (sqrt (+ (expt L 2) (expt y 2))))) ;OK
	(setq P00 (polar P10 (* PI -0.5) SF))
	(setq P01 (polar P11 (* PI -0.5) SF))
	(command "_.Line" P00 P10 ""
			"_.Offset" thk P00 P10 "")			
	(command "_.Line" P11 P01 ""
			"_.Offset" thk P11 P01 "")			
	(command "_.Arc" "_Non" P11 "_E" P21 "_R" KR
			"_.Offset" thk P11 P21 "")			
	(command "_.Arc" "_Non" P21 "_E" P20 "_R" CR
			"_.Offset" thk P21 P20 "")			
	(command "_.Arc" "_Non" P20 "_E" P10 "_R" KR
			"_.Offset" thk P20 P10 "")			
	(setq P101 (polar P10 (* PI 1) thk))
	(setq P111 (polar p11 (angle P10 IP) thk))	
	(command "_.Line" P111 P101 ""
			"_.Offset" SF P111 P101 "")
	(initcommandversion)
	(command "_.Join" "_All" "")
	(mapcar 'setvar lv vv)
  (princ)
)

 

 

Message 3 of 6

ВeekeeCZ
Consultant
Consultant

Welcome to the forum!

IMHO the code looks good. I wouldn't worry about a style too much. I would rather have simple code that I would understand easily than a stylish code which is hard to read. The funtionality is what matters. And there some things that you should add to improve that.

 

- turn off OSNAPs! (if you use "_non" prior the point, you need to add that prior all points. If there are too many, better just turn OSNAPs off entirely.)

- make sure you reset changed system varibales at the end (even in case of an error in your program) - add *error* function

- localize all variables - some variables are necessary! to localize eg. *error* !!

- take care of user input. Allow only values that are meaningful. (can be ID zero or negative?) - 

 

- are you aware that (/ integer integer) is an integer? Consider using real numbers even if just for visual assurance

- you can add an UNDO wraper

 

- about join - use a selection-set to collect all the lines to be about to join...

 

(defun c:SE (/ *error* osm doc ss2join IP ID Z
	     ...
	     )

    (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if osm (setvar 'OSMODE osm))
    (vla-endundomark doc)
    (princ))

  (vla-startundomark (setq doc (vla-get-activedocument (vlax-get-acad-object))))
  (setq osm (getvar 'OSMODE))
    
  (setq IP (getpoint "\nSpecify Insertion Point : "))
  (initget (+ 1 2 4))
  (setq ID (getdist "\nInner Diameter : "))
  
...  
  (setq Z (- (/ ID 2.) KR))  
...
  
  (setq ss2join (ssadd)) ;create an empty ss
  (setvar 'OSMODE 0)
  
  (command "Line" P00 P10 "")
  (command "Offset" thk P00 P10 "")
  (ssadd (entlast) ss2join) ; add last to ss.
     
  (command "Arc" P11 "_E" P21 "_R" KR) 
  (command "Offset" thk P11 P21 "") ; now you don't need "_non"
  (ssadd (entlast) ss2join)
  
...  
  (initcommandversion)
  (command "_.Join" ss2join "")
  
  (*error* "end")
  )
Message 4 of 6

devitg
Advisor
Advisor

Please , would you give real values for 

 

IP
ID 
SF 
THK

And , if possible , a sample dwg where you apply the lisp.

Thanks 

 

0 Likes
Message 5 of 6

Anonymous
Not applicable

Dear BeeKeeCZ, your advises are very valuable for me!

I will revise my coding based on this!

Really thank you very very much!

0 Likes
Message 6 of 6

Anonymous
Not applicable

Dear Frjuniornogueira,

Thank you very much, the coding works great!

May I know the purpose to turn off and on the input echoing (cmdecho)?

 

0 Likes