LISP for sending objects to layers keeps freezing in the block editor

LISP for sending objects to layers keeps freezing in the block editor

matthew_neesley
Collaborator Collaborator
1,513 Views
13 Replies
Message 1 of 14

LISP for sending objects to layers keeps freezing in the block editor

matthew_neesley
Collaborator
Collaborator

Hello to all:

 

I am trying to revisit some LISP programming I've done in the past, and I'm having a familiar problem; when I use the attached LISP on the attached file, AutoCAD stops with the block editor open.  I've gotta raise the white flag again and ask for help once more.  I've used all the individual "concepts" before in other LISPS, but piecing this one in particular together is at a standstill.  Thanks in advance for any help!

0 Likes
Accepted solutions (1)
1,514 Views
13 Replies
Replies (13)
Message 2 of 14

matthew_neesley
Collaborator
Collaborator

 Hmmm...seems that the following line is what's causing the holdup...

 

(setq vnum (/ (length (vlax-get b 'coordinates)) 2))

 

I'll have to investigate more thoroughly tomorrow (but if anyone still wants to contribute, I'm all ears!)

 

0 Likes
Message 3 of 14

matthew_neesley
Collaborator
Collaborator

In this segment...how to I get AutoCAD to include LINES as well?

 

(if (setq a (ssget "_x" '((0 . "*polyline,"))))
;; selecting all lines & inside block in block editor
(progn
(repeat (setq i (sslength a))
(setq
b (vlax-ename->vla-object (ssname a (setq i (1- i))))
)

 

I can't use a wildcard like *line..because then the program will grab SPLINES as well, and that's part of the problem.

I've split the program into two "gathering" segments, one for splines, then one for polylines and lines; it's getting the LINES into the filtering that is my last headache here.

0 Likes
Message 4 of 14

matthew_neesley
Collaborator
Collaborator

Well...this one should do it!  What a ride.  I would be interested in hearing from anyone on how to clean/tidy-up/condense this so it's not quite so long...if possible.  Thanks in advance!

0 Likes
Message 5 of 14

dbhunia
Advisor
Advisor

change this.....

(setq vnum (/ (length (vlax-get b 'coordinates)) 2))

To

(setq vnum (/ (vla-get-length b) 2))

 

And also.....

(if (setq a (ssget "_x" '((0 . "*line"))))

To

(if (setq a (ssget "_x" '((0 . "line,LWPOLYLINE"))))

 

then try it.......Actually I do not understand what your code deos..... 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 6 of 14

dbhunia
Advisor
Advisor

Try this.....

 

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 7 of 14

ronjonp
Advisor
Advisor
Accepted solution

One quick thing I see is you don't need to open the block editor to modify these blocks, just go directly to the block definition and make your changes. Here's a quick untested example with comments.

;;*************************  { ADD PROPERTIES TO STANDARD CURV }  ****************************;;
;;                                                                                            ;;
;;       ------------------  Designed & Created by Satish Rajdev  ------------------          ;;
;;                                                                                            ;;
;;       ------------------  Command to Invoke = "9045-LH"-----------------                   ;;
;;                                                                                            ;;
;;********************************************************************************************;;

(defun c:9045-lh (/ vnum addprop bks cmd nm bkl a b c d e i)
  ;;********************************************************************************************;;
  ;;****************************************  UTILITIES ****************************************;;
  ;;********************************************************************************************;;
  (defun *error* (msg)
    (if	(not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
      (progn (princ "")
	     ;; RJP - removed command calls
	     ;;(setvar 'nomutt nm)
	     ;;(setvar 'cmdecho cmd)
	     (vla-endundomark (vla-get-activedocument (vlax-get-acad-object)))
      )
    )
    (princ)
  )
  ;; RJP - added function to return a list of block definition objects
  (defun _mbd (name / e r)
    (cond ((setq e (tblobjname "block" name))
	   (setq e (cdr (assoc 330 (entget e))))
	   (vlax-for x (vlax-ename->vla-object e) (setq r (cons x r)))
	  )
    )
  )
  ;; RJP - not needed, see below
;;;  (defun removedup (l)
;;;    (if	l
;;;      (cons (car l) (removedup (vl-remove (car l) (cdr l))))
;;;      ;; removing duplicate element from the list
;;;    )
;;;  )
  (defun addprop (obj layer)
    (vla-put-color obj 256)
    ;; put color to bylayer
    (vla-put-layer obj layer)
    ;; put in the layer specifed
  )
  (defun degrees->radians (numberofdegrees) (* pi (/ numberofdegrees 180.0)))
  ;;********************************************************************************************;;
  ;;**************************************  MAIN PRAGRAM ***************************************;;
  ;;********************************************************************************************;;
  (if (setq bks (ssget '((0 . "insert"))))
    ;; select block on screen
    (progn (vla-startundomark (vla-get-activedocument (vlax-get-acad-object)))
	   (mapcar '(lambda (x y)
		      (or (tblsearch "layer" x) ; verifies layer are present or not
			  (entmakex (list '(0 . "layer")
					  (cons 100 "AcDbSymbolTableRecord")
					  (cons 100 "AcDbLayerTableRecord")
					  (cons 2 x)
					  ;; add layername
					  (cons 70 0)
					  (cons 62 y)
					  ;; add color
					  (cons 6 "Continuous")
				    )
			  )
		      )
		      ;; creating layer
		    )
		   (list "HILMOT-CONV-BLK-ROLLERS"
			 "HILMOT-CONV-BLK-SENSORS"
			 "HILMOT-CONV-BLK-FRAMES"
			 "HILMOT-CONV-BLK-FULL_WIDTH_BELTS"
			 "HILMOT-CONV-BLK-DRIVE_CARDS"
			 "HILMOT-CONV-BLK-MDR'S"
			 "HILMOT-CONV-BLK-FLOW_ARROWS"
			)
		   ;; layer list
		   (list 8 1 4 3 5 6 7)
		   ;; color code for the layers
	   )
	   (repeat (setq i (sslength bks))
	     ;; RJP - Just check if the name is not in the list while compling then no need to remove duplicates
	     ;; setting the repeat count
	     (or (vl-position (setq n (cdr (assoc 2 (entget (ssname bks (setq i (1- i))))))) bkl)
		 (setq bkl (cons n bkl))
	     )
	     ;; getting the selected block list
	   )
	   ;; (setq bkl (removedup bkl))
	   (foreach bk bkl
	     (foreach b	(_mbd bk)
	       ;; selecting all lines, splines and plines inside block in block editor
					;(setq vnum (/ (length (vlax-get b 'coordinates)) 2))
	       ;; getting each ellipse/all line objects
	       (cond ;; applying conditions here
		     ((and (eq (vla-get-objectname b) "AcDbSpline")
			   (vlax-curve-isclosed b)
			   (< (vla-get-numberofcontrolpoints b) 20)
			   ;;the object is a spline, closed & has less than 20 control points then do this
		      )
		      (addprop b "HILMOT-CONV-BLK-FRAMES")
		     )
		     ((and (eq (vla-get-objectname b) "AcDbSpline")
			   (vlax-curve-isclosed b)
			   (> (vla-get-numberofcontrolpoints b) 20)
			   ;;the object is a spline, closed & has more than 20 control points then do this
		      )
		      (vla-delete b)
		     )
		     ((and (eq (vla-get-objectname b) "AcDbSpline")
			   (vlax-curve-isclosed b)
			   (> (vla-get-numberofcontrolpoints b) 13)
			   (< (vla-get-numberofcontrolpoints b) 15)
			   ;;the object is a spline, NOT closed & has 14 control points then do this
		      )
		      (addprop b "HILMOT-CONV-BLK-FLOW_ARROWS")
		     )
		     ((and (eq (vla-get-objectname b) "AcDbPolyline")
			   (vlax-curve-isclosed b)
			   (equal (/ (length (vlax-get b 'coordinates)) 2) 4)
		      )
		      ;; if the object is POLYLINE with 4 vertices and it is closed then do this
		      (addprop b "HILMOT-CONV-BLK-SENSORS")
		     )
		     ((and (eq (vla-get-objectname b) "AcDbPolyline")
			   (not (vlax-curve-isclosed b))
			   (equal (/ (length (vlax-get b 'coordinates)) 2) 8)
		      )
		      ;; if the object is POLYLINE with 8 vertices and it is NOT closed then do this
		      (addprop b "HILMOT-CONV-BLK-SENSORS")
		     )
		     ((and (eq (vla-get-objectname b) "AcDbPolyline")
			   (not (vlax-curve-isclosed b))
			   (equal (/ (length (vlax-get b 'coordinates)) 2) 4)
		      )
		      ;; if the object is POLYLINE with 4 vertices and it is NOT closed then do this
		      (vla-delete b)
		     )
		     ((and (eq (vla-get-objectname b) "AcDbPolyline")
			   (not (vlax-curve-isclosed b))
			   (equal (/ (length (vlax-get b 'coordinates)) 2) 7)
		      )
		      ;; if the object is POLYLINE with 7 vertices and it is NOT closed then do this
		      (addprop b "HILMOT-CONV-BLK-DRIVE_CARDS")
		     )
		     ((and (eq (vla-get-objectname b) "AcDbLine")
			   ;; check object is LINE
			   (setq c (vlax-get b 'startpoint))
			   ;; Get start point
			   (setq d (vlax-get b 'endpoint))
			   ;; Get end point
			   (vl-some '(lambda (x) (equal (angle c d) x 0.00001))
				    '(1.11563763 4.20761659 0.33023947 3.42221842)
				    ;; this angle list are measured manually from drawing from GREEN LINES
			   )
			   ;; Check whether the angle between start & end point are matches with the list if matches then do this
		      )
		      (addprop b "HILMOT-CONV-BLK-MDR'S")
		     )
		     (t (addprop b "HILMOT-CONV-BLK-ROLLERS"))
	       )
	     )
	   )
	   ;; RJP - No more command call so we don't have to set these
;;;	   (setvar 'nomutt nm)
;;;	   (setvar 'cmdecho cmd)
	   ;;restoring the variables again
	   (vla-endundomark (vla-get-activedocument (vlax-get-acad-object)))
	   ;;ending the undo mark
    )
    ;; progn
  )
  ;; if
  (princ)
)

(vl-load-com)
(princ)
(princ (strcat "\n:: Add Prop to Standard Curve.lsp ::"
	       "\n:: Created by Satish Rajdev | "
	       (menucmd "M=$(edtime,$(getvar,date),DDDD\",\" D MONTH YYYY)")
	       " ::"
	       "\n:: Type \"9045-LH\" to Invoke ::"
       )
)
(princ)
Message 8 of 14

matthew_neesley
Collaborator
Collaborator
Thanks very much for the reply! I was looking into that based on a reply
to a different message from a while ago, but your reply makes all that much
easier! 🙂
0 Likes
Message 9 of 14

matthew_neesley
Collaborator
Collaborator

Hey, that is all very slick!  I ran it, works good, except for one nagging little thing: the dwg isn't refreshing automatically.  How can I do that?  I've tried using this towards the end:

    

(vla-Regen doc acAllViewports)

 

I have not been successful. Any thoughts?

0 Likes
Message 10 of 14

matthew_neesley
Collaborator
Collaborator

(vla-regen (vla-get-activedocument (vlax-get-acad-object))
acAllViewports

0 Likes
Message 11 of 14

ronjonp
Advisor
Advisor

In your test drawing adding (vla-Regen doc acAllViewports) works here. Perhaps it a graphics card issue?

2018-12-12_12-51-54.gif

0 Likes
Message 12 of 14

ronjonp
Advisor
Advisor

Another thing I noticed is you need to add (NOT ) to this line per your comment. This will never get evaluated as it stands now 🙂

image.pngMay something like this:

((and (eq on "AcDbSpline")
			   (not (vlax-curve-isclosed b))
			   (= (vla-get-numberofcontrolpoints b) 14)
			   ;;the object is a spline, NOT closed & has 14 control points then do this
		      )
		      (addprop b "HILMOT-CONV-BLK-FLOW_ARROWS")
		     )
0 Likes
Message 13 of 14

matthew_neesley
Collaborator
Collaborator

HAHA yup...I will admit that at least of the "descriptions" don't actually match what I want them to do...haven't gotten to cleaning that up yet.  Aren't you an eagle eye 🙂

0 Likes
Message 14 of 14

ronjonp
Advisor
Advisor

@matthew_neesley wrote:

HAHA yup...I will admit that at least of the "descriptions" don't actually match what I want them to do...haven't gotten to cleaning that up yet.  Aren't you an eagle eye 🙂


Good job taking the time to document your code. It's an important part IMO. *Cheers!

0 Likes