Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Use this lisp for every selected objects - Divide

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
Anonymous
2879 Views, 10 Replies

Use this lisp for every selected objects - Divide

Hi everyone,
then I would have a problem. I found a lisp that divides a line into n lines.
I would like to use this lisp for every selected objects (lines or polylines), but currently only works for a selected object.
Can someone help me?

Thank you very much in advance.

I attach to follow the lisp.

_____________________________________

;; SubDivide.lsp [command name: SD]
;; To SubDivide a finite linear object into a specified
;; number of equal-length contiguous sections.
;; Option: numerical quantity of sections, or Maximum
;; length of sections, however many are required.
;; Can also place Points at subdivision locations, if
;; desired [see commented-out Divide command].
;; Remembers choices and offers as defaults.
;; Works on anything of finite length that Break can
;; be used on [not Xlines or Rays].
;; Kent Cooper, last edited September 2011
;
(defun C:SD
(/ *error* subdiv obj cmde blips osm esel ent etype
entlen secdef sectemp maxtemp secs seclen ent2 secs1 secs2)
;
(defun *error* (errmsg)
(if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
(princ (strcat "\nError: " errmsg))
); end if
(setvar 'osmode osm)
(setvar 'blipmode blips)
(command "_.undo" "_end")
(setvar 'cmdecho cmde)
); end defun *error*
;
(defun subdiv (obj brks)
(repeat brks
(command
"_.break"
obj
(trans (vlax-curve-getPointAtDist obj seclen) 0 1)
"@"
); end command
(setq obj (entlast))
); end repeat
); end defun - subdiv
;
(vl-load-com)
(setq cmde (getvar 'cmdecho))
(setvar 'cmdecho 0)
(command "_.undo" "_begin")
(setq
blips (getvar 'blipmode)
osm (getvar 'osmode)
); end setq
;
(while
(not
(and
(setq esel (ssget ":S" '((0 . "LINE,ARC,CIRCLE,*POLYLINE,ELLIPSE,SPLINE"))))
(= (cdr (assoc 70 (tblsearch "layer" (cdr (assoc 8 (entget (ssname esel 0))))))) 0)
; 0 for Unlocked, 4 for Locked
); end and
); end not
(prompt "\nNothing selected, or not a finite path type, or on a Locked Layer; try again:")
); end while
(setvar 'osmode 0)
(setvar 'blipmode 0)
(setq
ent (ssname esel 0)
etype (if ent (cdr (assoc 0 (entget ent))))
entlen (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent))
); end setq
;
(initget 6 "Maximum"); no zero, no negative
(setq
secdef (if _sdsec_ _sdsec_ "Maximum")
; once set, _sdsec_ is either integer or "Maximum" [first-use default]
sectemp
(getint ; [returns nil on Enter]
(strcat
"\nEnter number of Sections, or M for Maximum length <"
(if (numberp secdef); if default is a number,
(itoa secdef); then - text equivalent
secdef ; else - it's "Maximum"; use it
); end if
">: "
); end strcat
); end getint and sectemp
_sdsec_ (if sectemp sectemp secdef)
; what User typed if other than Enter - if Enter, use default
); end setq
(if (= _sdsec_ "Maximum")
(while
(not
(and
(progn
(if _sdmax_ (initget 38) (initget 39)); returns nil, so need (progn) wrapper
; no Enter on first use, no 0, no negative, dashed rubber-band if picked on-screen
(setq
maxtemp
(getdist
(strcat
"\nMaximum length of Sections"
(if _sdmax_ (strcat " <" (rtos _sdmax_) ">") ""); no default on first use
": "
); end strcat
); end getdist & maxtemp
_sdmax_ (cond (maxtemp) (_sdmax_))
; if User typed/picked something other than Enter, then - use it; else - default
); end setq
); end progn
(<= _sdmax_ entlen); Maximum length shorter than or equal to length of object
); end and
); end not
(prompt "\nObject is no longer than Maximum length.")
); end while
); end if
(setq secs
(if (numberp _sdsec_)
_sdsec_ ; then [number]
(if (zerop (rem entlen _sdmax_)); else [Maximum]
(fix (/ entlen _sdmax_))
(1+ (fix (/ entlen _sdmax_)))
); end if and else
); end if
seclen (/ entlen secs)
); end setq
;
; (command "_.divide" ent secs) ; remove leading semicolon and this comment if Points are desired along with subdivision
(cond
( ; Circle or closed Ellipse/Spline [can't break at one point]
(and (wcmatch etype "CIRCLE,ELLIPSE,SPLINE") (vlax-curve-isClosed ent))
(command "_.copy" ent "" "0,0" "0,0"); make a copy in place
(setq
ent2 (entlast)
secs1 (/ secs 2)
secs2 (- secs secs1)
); end setq
(command
"_.break"
ent
(trans (vlax-curve-getStartPoint ent) 0 1)
(trans (vlax-curve-getPointAtDist ent (* seclen secs1)) 0 1)
"_.break"
ent2
(trans (vlax-curve-getPointAtDist ent2 (* seclen secs1)) 0 1)
(trans (vlax-curve-getStartPoint ent2) 0 1)
); end command
(subdiv ent (1- secs2))
(subdiv ent2 (1- secs1))
); end Circle or closed Ellipse/Spline condition
(T ; everything else
(subdiv ent (1- secs))
); end everything-else condition
); end cond
;
(setvar 'osmode osm)
(setvar 'blipmode blips)
(command "_.undo" "_end")
(setvar 'cmdecho cmde)
(princ)
); end defun
(prompt "Type SD to SubDivide a linear object.")

10 REPLIES 10
Message 2 of 11
pbejse
in reply to: Anonymous

Are you wanting to have this work on multiple selections? Is that all you want to do?

 

EDIT: Ooops , mis read the topic [ Removed attachment ]

 

 

 

Message 3 of 11
Anonymous
in reply to: pbejse

Exact.
Thank you so much pbejse. I tried it.
It works perfectly!

Message 4 of 11
pbejse
in reply to: Anonymous


@Anonymous wrote:

Exact.
Thank you so much pbejse. I tried it.
It works perfectly!


 

I see Smiley Wink but hang on.. There's a bug in code, I'll have a look later dude.

 

 

Message 5 of 11
Anonymous
in reply to: pbejse

Ah OK. It seemed to work properly.
I will wait impatiently.
Thank you very much again.

Message 6 of 11
Kent1Cooper
in reply to: Anonymous

Here's my update for Multiple-object selection.

 

In the case of the Maximum-length option, when any object in the selection isn't longer than that maximum length, it simply doesn't do anything to it.  It doesn't report at the end whether there were any that were not subdivided, but if that seems important, it could be made to do that.

 

This was edited from a later version of the original [last edited Dec. 2016, but I don't recall what the difference is between that and the 2011 version], so there may be other improvements.

Kent Cooper, AIA
Message 7 of 11
pbejse
in reply to: Kent1Cooper


@Kent1Cooper wrote:

Here's my update for Multiple-object selection.

 

In the case of the Maximum-length option, when any object in the selection isn't longer than that maximum length, it simply doesn't do anything to it.  It doesn't report at the end whether there were any that were not subdivided, but if that seems important, it could be made to do that.

...


 

There you go.  That is what i was referring to. 

Nice.

I was planning to take out that option altogether, as the OP doesnt seem to need that option 

 

 

pBe

 

 

Message 8 of 11
Kent1Cooper
in reply to: Kent1Cooper


@Kent1Cooper wrote:

....

In the case of the Maximum-length option, when any object in the selection isn't longer than that maximum length, it simply doesn't do anything to it.  It doesn't report at the end whether there were any that were not subdivided, but if that seems important, it could be made to do that.

....


 

Here's a version that reports that.  And I took the M off the file name, since I didn't see much point in keeping around separate versions for single- vs. multiple-object selection.

Kent Cooper, AIA
Message 9 of 11
40901305
in reply to: Kent1Cooper

Hi Kent, could you please add gaps between divided section.

Message 10 of 11

Thank you! I will give it a try!

Message 11 of 11
Kent1Cooper
in reply to: 40901305


@40901305 wrote:

Hi Kent, could you please add gaps between divided section.


Spurred by a similar request elsewhere, I finally got around to this.  See the attached [same file name and command name -- just now with the option to have gaps].

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report