Simplifying code

Simplifying code

Anonymous
Not applicable
1,152 Views
7 Replies
Message 1 of 8

Simplifying code

Anonymous
Not applicable

I have gathered bits and pieces of code (As I do not yet know how to code properly in LISP) which I want to simplify so that I only have to select multiple points once and it does both commands.

The problem with the first command "SPOT" is that I'm not given the option to select my points which I want to be able to do, and then use the exact some list of points for the second command without having to select them again.

 

(defun C:SPOT ( / sset pt ent loc pt2 elv i)
  (setq sset (ssget "_X" '((0 . "POINT")))
	i 0
	z (getvar "textsize")
  )
  (if sset
    (repeat (sslength sset)
      (setq pt (ssname sset i))
      (setq ent (entget pt))
      (setq loc (cdr (assoc 10 ent)))
      (setq pt2 (polar (list (car loc) (cadr loc)) 0.0 z))
      (setq elv (last loc))
      (entmake (list
	        (cons 0 "TEXT")
		(cons 100 "AcDbText")
	        (cons 8 "SPOTLEVELS")		;Layer
		(cons 1 (rtos elv 2 2))		;Content
	        (cons 10 pt2)			;Insert Point
	        (cons 40 0.4)			;Text Height
		(cons 41 0.8)			;Text Width
	        (cons 71 1)			;Justification (4=Top Left)
		(cons 50 0.785398)		;Rotation (Radians)
		(cons 51 0.261799)		;Oblique Angle
		(cons 7 "R")			;Text Style
		(cons 100 "AcDbText")
	       )
      )
      (setq i (1+ i))
    )
    (princ "\n No POINT objects found!   ")
  )  
  (princ)
)
(defun c:cross ( / BlockName ss sn)
  ;; Tharwat - Date: 28.June.2016 ;;
  (setq BlockNAme "SH_CROSS") ;; Replace ABC with your Block Name and be sure it is not Attributed Block.
  (if (and (or (tblsearch "BLOCK" BlockName)
               (progn
                 (princ (strcat "\nBlock name <" BlockName "> is not found in this drawing !")) nil)
               )
           (princ (strcat "\nSelect points to place the block name < " BlockName " > at their insertion point :"))
           (setq ss (ssget '((0 . "POINT"))))
           )
    (while (setq sn (ssname ss 0))
      (entmake (list '(0 . "INSERT") (cons 2 BlockName) (assoc 10 (entget sn))))
      (ssdel sn ss)
      )
    )
  (princ)
)
(defun c:SPOTHEIGHT()
 (c:SPOT)
 (c:cross)
 (princ)
)

  

0 Likes
Accepted solutions (1)
1,153 Views
7 Replies
Replies (7)
Message 2 of 8

kerry_w_brown
Advisor
Advisor

 

Which list to you want to re-use.

 

Can we assume it is a list of the loc  values

 

You could try making a list by appending the loc variable to a list as you step through the selection set.

assign the list to a global variable .. suitably named.

Then test for and use the variable in c:cross. and set it to nil when complete.

 

Personally I'd combine the 2 functions into one and do your cross mojo inside spot .. rename the function if necessary.

 

Don't be concerned about optimising the code or making it pretty or really efficient ... just get it working. All the pretty and efficient stuff will come later when you start to become proficient.

 

Regards,

 

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 3 of 8

Anonymous
Not applicable

The global variable sounds like a good idea, and combing the two commands in a better way.

But the problem I'm having at the moment is that for the first function, I'm don't have the ability to select specific 3Dpoints i want but instead just does all of them, which is not what I want. I'd like to choose the points I want and use those same points for both functions if that makes sense. The main problem I have also is I don't have enough experience to code in LISP yet, only bits and pieces.

0 Likes
Message 4 of 8

dbroad
Mentor
Mentor

To allow you to select which points to use, take out the "X" from the ssget.  To allow you to reuse either the selection set or the resulting list, save the results in a relatively uniquely named global variable.  Warning:  If you decide to make the selection set global, you should delete it at some point as AutoCAD cannot have but so many open selection sets

 

(prompt "\nInform your users about what to select");Without "x", user will need to decide what to select.
(setq spotset (ssget  '((0 . "POINT"))));allow you to select points
Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 5 of 8

Anonymous
Not applicable

How would I create a global variable from the selected list and use it in both? or if it's the better option, merge the 2 commands together

Sorry for the trouble, If I had the knowledge of Lisp I would change all the code myself

0 Likes
Message 6 of 8

dbroad
Mentor
Mentor

Any symbol name that is not listed in the functions (arguments / local variables) list just after the defun and name will be global.  Make the variable unique as I demonstrated so that its value is not accidentally overwritten by other programs.

 

I am not available for free programming but will be glad to help you make the changes yourself.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 7 of 8

zph
Collaborator
Collaborator

Would the VL-PROPAGATE function accomplish the task at hand?

 

EDIT:  nevermind, this handles variables across drawing files....

0 Likes
Message 8 of 8

joselggalan
Advocate
Advocate
Accepted solution

Very fast. (I have no time)

 

check the code.
Note that the global variable "sset" is used in both functions and closes in the main function.

Remove "X" from ssget as you indicated dbroad.

..and remove the code block selection.

 

(defun SPOT ( / pt ent loc pt2 elv i sty)
  (prompt "\nSelect points: ")
  (setq sset (ssget '((0 . "POINT")))
	i 0
	z (getvar "textsize")
  )
  (setq sty "R")
  (if (not (tblsearch "STYLE" sty))
   (setq sty (getvar 'textstyle))
  )
  (if sset
    (repeat (sslength sset)
      (setq pt (ssname sset i))
      (setq ent (entget pt))
      (setq loc (cdr (assoc 10 ent)))
      (setq pt2 (polar (list (car loc) (cadr loc)) 0.0 z))
      (setq elv (last loc))
      (entmake (list
	        (cons 0 "TEXT")
		(cons 100 "AcDbText")
	        (cons 8 "SPOTLEVELS")		;Layer
		(cons 1 (rtos elv 2 2))		;Content
	        (cons 10 pt2)			;Insert Point
	        (cons 40 0.4)			;Text Height
		(cons 41 0.8)			;Text Width
	        (cons 71 1)			;Justification (4=Top Left)
		(cons 50 0.785398)		;Rotation (Radians)
		(cons 51 0.261799)		;Oblique Angle
		(cons 7 sty)			;Text Style
	       )
      )
      (setq i (1+ i))
    )
    (princ "\n No POINT objects found!   ")
  )  
  (princ)
)
(defun cross ( / BlockName sn)
  (setq BlockNAme "SH_CROSS") 
  (if (not (tblsearch "BLOCK" BlockName))
    (prompt (strcat "\nBlock name <" BlockName "> is not found in this drawing !"))
    (while (setq sn (ssname sset 0))
      (entmake (list '(0 . "INSERT") (cons 2 BlockName) (assoc 10 (entget sn))))
      (ssdel sn sset)
    )
  )
  (princ)
)
(defun c:SPOTHEIGHT ( / sset)
 (SPOT)
 (if sset (cross))
 (princ)
)

regards...