Insert finish level (y-position) 2d elevation lisp cleanup

Insert finish level (y-position) 2d elevation lisp cleanup

hamza_itani
Enthusiast Enthusiast
293 Views
3 Replies
Message 1 of 4

Insert finish level (y-position) 2d elevation lisp cleanup

hamza_itani
Enthusiast
Enthusiast

Is there a need to clean up this lisp? It's working fine but feels over bloated.

I need it to keep allowing the user to visually scale the block before insertion (since scaling it after insertion will result in wrong y-position value).

Plus, currently it gives unique names to each created block, which is necessary because each created block will have a different elevation base (datum), so I need that feature to stay.

 

(defun dtr (x)
  (* pi (/ x 180.0))
)

(defun c:flv (/ elv1 ss0 pp0 oldmode oldlayer ss1 ss2 ss3 ss4 ss5 pt1 pt2 pt3 ll1 ll2 xscale vObjid)

  (command "_.undo" "_begin")

  (setq otstyle (getvar "textstyle"))

  (if (setq elv1 (getreal "\nSpecify Elevation <0.0>: ")) nil (setq elv1 0.0 ))

  (if (setq pp0  (getpoint (strcat "Specify Base point of " (rtos elv1 2 2 ) "  or <0,0,0>:") )) nil (setq pp0 (list 0 0 0)) )

  (setq ss0 1.0)

  (setq oldmode (getvar "osmode"))
  (setvar "osmode" 0)
  (setq oldlayer (getvar "clayer"))

  (command "-layer" "m" "FLV" "c" "10" "" "LW" "0.13" "" "" )

  (setq pt0 (- (nth 1 pp0) elv1))
  (setq pt1 (list 0.0 pt0 0.0))
  (setq pt2 (polar pt1 (dtr 135.0) (* ss0 1)))
  (setq pt3 (polar pt1 (dtr 45.0) (* ss0 1)))
  (command "solid" pt1 pt2 pt3 "" "")
  (setq ss1 (entlast))

  (setq ll1 (polar pt2 (dtr 90.0) (* ss0 0.03)))
  (setq ll2 (polar ll1 (dtr 0.0) (* ss0 4)))
  (command "line" ll1 ll2 "" )
  (setq ss2 (entlast))

  (command "_copybase" (list 0 0 0 ) ss1 ss2 "" )
  (command "_pasteblock" (list 0 0 0) )

  (setq ss3 (entlast))
  (setq vObjid (vla-get-ObjectID (vlax-ename->vla-object ss3 )))
  (command "_erase" ss1 ss2 "" )
  (command "-style" "FLV" "arial" "0" "1" "0" "n" "n" )
  (command "-text" "j" "br" ll2 ss0 "0" "FLV" )

  (setq ss4 (entlast))
  (command "_copybase" pt1 ss3 ss4 "" )
  (command "_erase" ss3 ss4 "" )
  (command "_pasteblock" pp0 )

  (princ "\nSpecify scale (hit Enter to use last scale): ")

  (command "SCALE" "LAST" "" pp0 "_non" pause)

  (setq ss5 (entlast))

  ;; Store the X scale of the last created block
  (setq xscale (cdr (assoc 41 (entget ss5))))

  ;; Erase ss5
  (command "_erase" ss5 "" )

  ;; Redo the process with the stored scale value
  (setq ss0 xscale)

  (setq pt0 (- (nth 1 pp0) elv1))
  (setq pt1 (list 0.0 pt0 0.0))
  (setq pt2 (polar pt1 (dtr 135.0) (* ss0 1)))
  (setq pt3 (polar pt1 (dtr 45.0) (* ss0 1)))
  (command "solid" pt1 pt2 pt3 "" "")
  (setq ss1 (entlast))

  (setq ll1 (polar pt2 (dtr 90.0) (* ss0 0.03)))
  (setq ll2 (polar ll1 (dtr 0.0) (* ss0 4)))
  (command "line" ll1 ll2 "" )
  (setq ss2 (entlast))

  (command "_copybase" (list 0 0 0 ) ss1 ss2 "" )
  (command "_pasteblock" (list 0 0 0) )

  (setq ss3 (entlast))
  (setq vObjid (vla-get-ObjectID (vlax-ename->vla-object ss3 )))
  (command "_erase" ss1 ss2 "" )
  (command "-style" "FLV" "arial" "0" "1" "0" "n" "n" )
  (command "-attdef" "" "FLV" "" (strcat "%<\\AcObjProp.16.2 Object(%<\\_ObjId \"" (itoa vObjid) "\">%,1).InsertionPoint \\f \"%lu2%pt2%pr2\">%") "j" "br" ll2 ss0 "0" )

  (setq ss4 (entlast))
  (command "_copybase" pt1 ss3 ss4 "" )
  (command "_erase" ss3 ss4 "" )
  (command "_pasteblock" pp0 )

  (setvar "osmode" oldmode)
  (setvar "clayer" oldlayer )
  (setvar "textstyle" otstyle)

  (command "_.undo" "_end")

  (princ)
)

 

0 Likes
Accepted solutions (1)
294 Views
3 Replies
Replies (3)
Message 2 of 4

Moshe-A
Mentor
Mentor

@hamza_itani ,

 

what's the point on creating each time a new block using random name?

why not create a good elevation block (spare the lisp)

 

Moshe

 

0 Likes
Message 3 of 4

hamza_itani
Enthusiast
Enthusiast

The lisp makes it easier to reset the base elevation for each group of section drawings, since I use it to spread elevations for several road and building sections, and they don't all share the same ground level.

That's why each newly created block will have a different name and a different datum.

Any advice whether to simplify the lisp, keep it as is, or use a different approach?

0 Likes
Message 4 of 4

ВeekeeCZ
Consultant
Consultant
Accepted solution

If you are changing sysvar, ALWAYS add *ERROR* function to restore them in case of  error or simple ESC break.

 

Utilize variable names.

ss usually represents a selection set.

your ss0 is an integer?

your ss1+ are enames?

 

All strings within (command... "1" "2")... better use integers right away.

(list 0 0 0)... use just '(0 0 0) or '(0 0) in 2D

 

All 0.0 could be just 0. if you want to save yourself a stroke.

 

Learn to work in radians directly and don't use degrees.

 

(princ "\nSpecify scale (hit Enter to use last scale): ")

Use common formats, don't create your own

(princ "\nSpecify scale <use last>: ")
or better use the actual value
(princ (strcat "\nSpecify scale <" (itoa ss0) ">: "))

 

(if (setq elv1 (getreal "\nSpecify Elevation <0.0>: ")) nil (setq elv1 0.0 ))
often you see:
(if (not (setq elv1 (getreal "\nSpecify Elevation <0.0>: "))) (setq elv1 0.0 ))
or more clear
(setq elv1 (getreal "\nSpecify Elevation <0.0>: "))
(if (not elv1) (setq elv1 0.))

 

Do you really need all those variables?
(setq ll1 (polar pt2 (dtr 90.0) (* ss0 0.03)))
(setq ll2 (polar ll1 (dtr 0.0) (* ss0 4)))
(command "line" ll1 ll2 "" )

(command "line"
        (polar pt2 (/ pi 2) (* ss0 0.03)))
        (polar ll1 0. (* ss0 4))
         "")

 

You could rewrite it to use half of the variables. 

 

sometimes you use "_erase", other times just "solid", why?

 

Somewhere you add "_non", somewhere you do not. So it's missing or superfluous?

 

Why do you capitalize these?
(command "SCALE" "LAST" "" pp0 "_non" pause)
Should be something else also capitalized?