Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Active Member
Serichy76
Posts: 8
Registered: ‎03-23-2012
Message 1 of 14 (852 Views)

Lisp routine required to replace/place a block on each closed polyline?

852 Views, 13 Replies
03-26-2012 04:11 AM

Does anyone know if there is a Lisp routine available to batch replace all the closed polylines with an existing block? Any help would be gratefully received...  : )

*Expert Elite*
pbejse
Posts: 2,459
Registered: ‎11-24-2009
Message 2 of 14 (847 Views)

Re: Lisp routine required to replace/place a block on each closed polyline?

03-26-2012 04:28 AM in reply to: Serichy76

Serichy76 wrote:

Does anyone know if there is a Lisp routine available to batch replace all the closed polylines with an existing block? Any help would be gratefully received...  : )


Yup. you could try searching the forum first. if you can't find one, THEN we'll make one.

 


 

Mentor
stevor
Posts: 844
Registered: ‎12-26-2005
Message 3 of 14 (846 Views)

Re: Lisp routine required to replace/place a block on each closed polyline?

03-26-2012 04:31 AM in reply to: Serichy76

If not, there will be.

 

What about the posibility of diffeent sizes, and orientations?

And one usually includes layer data.

S
*Expert Elite*
pbejse
Posts: 2,459
Registered: ‎11-24-2009
Message 4 of 14 (844 Views)

Re: Lisp routine required to replace/place a block on each closed polyline?

03-26-2012 04:36 AM in reply to: stevor

stevor wrote:

If not, there will be.

 



Indeed :smileylol:

 

 

Active Member
Serichy76
Posts: 8
Registered: ‎03-23-2012
Message 5 of 14 (827 Views)

Re: Lisp routine required to replace/place a block on each closed polyline?

03-26-2012 05:33 AM in reply to: pbejse

I did have an extensive search first before posting this message but no luck  : (

basically I have a number of different closed plines, some shaped into triangles for example. I need to place an existing block in the centre of each triangle, it really doesn't matter which way the triangle faces & the blocks can all be placed the same orientation, it's for location more than anything.

 

Many thanks for your time

 

Sally

*Expert Elite*
pbejse
Posts: 2,459
Registered: ‎11-24-2009
Message 6 of 14 (825 Views)

Re: Lisp routine required to replace/place a block on each closed polyline?

03-26-2012 05:39 AM in reply to: pbejse

Here's a starting point for you.

 

(defun Pl2oBlock (bn / *error* _Center old_os ss i)
  (vl-load-com)
  (defun *error* (msg)
    (command "._undo" "_end")
    (setvar 'osmode old_os)
    (setvar 'cmdecho 1)
  ) ;_ end_defun
  (defun _Center (e / mn mx)
    (vla-getboundingbox (vlax-ename->vla-object e) 'mn 'mx)
    (mapcar (function (lambda (a b) (/ (+ a b) 2.)))
	    (vlax-safearray->list mn)
	    (vlax-safearray->list mx)
    )
  )
  (setq old_os (getvar 'osmode))
  (setvar 'osmode 0)
  (command "._undo" "_begin")
  (if (and (tblsearch "BLOCK" bn)
	   (setq ss (ssget "_X"
			   '((0 . "LWPOLYLINE")
			     (-4 . "&=")
			     (70 . 1)
			     (410 . "MODEL")
			    )
		    )
	   )
      )
    (repeat (setq i (sslength ss))
      (vlax-invoke
	(vlax-get (vla-get-ActiveLayout
		    (vla-get-activedocument (vlax-get-acad-object))
		  )
		  'Block
	)
	'InsertBlock
	(_Center (ssname ss (setq i (1- i))))
	bn 1 1 1 0
      )
    )
  )
  (*error* "")
  (princ)
)

 

(PL2OBLOCK "BlockName")

 

HTH

 

Active Member
Serichy76
Posts: 8
Registered: ‎03-23-2012
Message 7 of 14 (820 Views)

Re: Lisp routine required to replace/place a block on each closed polyline?

03-26-2012 06:01 AM in reply to: pbejse

My apologies but I can't seem to get this to work, I've pasted the routine into a lisp file then APPLOAD into AutoCAD but the promt PL2oBlock isn't being recognised.

 

last week I did find a lisp to replace circles with blocks which works brilliantly (see attached). it works by having the desired block already on the page so the promt asks for the block first which u select, then asks you to select which circles to replace. Is there a way this could be modified to work with triangles?

 

Thank you so much for this.

*Expert Elite*
Kent1Cooper
Posts: 5,358
Registered: ‎09-13-2004
Message 8 of 14 (818 Views)

Re: Lisp routine required to replace/place a block on each closed polyline?

03-26-2012 06:09 AM in reply to: Serichy76

Serichy76 wrote:

I did have an extensive search first before posting this message but no luck  : (

basically I have a number of different closed plines, some shaped into triangles for example. I need to place an existing block in the centre of each triangle, it really doesn't matter which way the triangle faces & the blocks can all be placed the same orientation, it's for location more than anything.

 

Many thanks for your time

 

Sally


Here's my take on it [without some of the controls you could add, such as Object-Snap, error handling, etc.]:

 

(setq CPss
  (ssget "_X"
    '( ; find all Closed LWPolylines
      (-4 . "<and")
        (0 . "LWPOLYLINE")
        (-4 . "<or"); closed
          (70 . 1); without linetype generation
          (70 . 129); with linetype generation
        (-4 . "or>")
      (-4 . "and>")
    ); filter list
  ); ssget
); setq
(foreach pl (mapcar 'cadr (ssnamex CPss)); list of entity names
  (setq
    pldata (entget pl); entity data
    verts (cdr (assoc 90 pldata)); number of vertices
    verlist ; list of vertices
      (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) pldata))
    sum '(0 0); reset for each
  ); setq
  (foreach x verlist (setq sum (mapcar '+ x sum))); sum of X,Y of all vertices
  (command
    "_.insert"
    "YourBlockName" ; <---------------------- EDIT THIS
    (mapcar '/ sum (list verts verts)); average of vertex locations
    "" "" "" ; scales = 1, rotation = 0
  ); command
); foreach

Kent Cooper
*Expert Elite*
pbejse
Posts: 2,459
Registered: ‎11-24-2009
Message 9 of 14 (813 Views)

Re: Lisp routine required to replace/place a block on each closed polyline?

03-26-2012 06:27 AM in reply to: Serichy76

Serichy76 wrote:

My apologies but I can't seem to get this to work, I've pasted the routine into a lisp file then APPLOAD into AutoCAD but the promt PL2oBlock isn't being recognised.

 

Thank you so much for this.


The usage

(Pl2oBlock "Blockname")

 

I made it that way so the blcok name would be generic, as prepration for you script in the event that the drawing requries a different block for every opened drawing

 

_.open "C:\Folderpath\sample1.dwg" (Pl2oBlock "Blockname") _.save  _close
_.open "C:\Folderpath\sample2.dwg" (Pl2oBlock "Blockname") _.save  _close

  


Serichy76 wrote:

 

last week I did find a lisp to replace circles with blocks which works brilliantly (see attached). it works by having the desired block already on the page so the promt asks for the block first which u select, then asks you to select which circles to replace. Is there a way this could be modified to work with triangles?

 


As for that one , well.... you can check the authors website  LeeMac , knowing LM he probably have an updated version for that routien you attached.

 

HTH

 

 

 

*Expert Elite*
pbejse
Posts: 2,459
Registered: ‎11-24-2009
Message 10 of 14 (811 Views)

Re: Lisp routine required to replace/place a block on each closed polyline?

03-26-2012 06:30 AM in reply to: Kent1Cooper

Kent1Cooper wrote:

 

  (foreach x verlist (setq sum (mapcar '+ x sum))); sum of X,Y of all vertices
  (command
    "_.insert"
    "YourBlockName" ; <---------------------- EDIT THIS
    (mapcar '/ sum (list verts verts)); average of vertex locations
    "" "" "" ; scales = 1, rotation = 0
  ); command
); foreach


Nice Kent. :smileyhappy:

 

 

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.