Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

AutoCAD - Divide line with predefined gap

rafaelcmeyer
Explorer

AutoCAD - Divide line with predefined gap

rafaelcmeyer
Explorer
Explorer

Hi, everyone,

 

I would like to know if there is a way to divide a line in Autocad with a predefined gap. For example, I have a line with 10 meters that I have to split into 10 equal parts, so each part will have 1 meter. But, these parts must have a gap between them of 0,2 meters, therefore the correct measure of each part will be 0,82 meters. 

Example.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I usually do this manually, but is there a way to make this with a command? Something like: select the line, type the gap of 0,2, set the number of divisions needed, and then the division points appear automatically? 

 

Sorry if this has been asked before, I have searched the forum and I could not find a similar question. 

 

[ The subject line of this post has been edited to include the product name by @handjonathan ]

Reply
Accepted solutions (1)
1,245 Views
13 Replies
Replies (13)

Kent1Cooper
Consultant
Consultant

That would not be difficult with an AutoLisp routine, but more detail please.  What are you starting with?  In the image it's clearly not a Line as AutoCAD uses the word.  Is it a rectangle [a closed 4-segment Polyline]?  A single-line-segment Polyline with width?  Something else?  And by "divide" do you mean to just place things along it [as the DIVIDE command puts Points], or since you use the word "gap," actually divide it [break it up] into separate pieces?  If placing things along it, what are they?  Is it always a straight open-ended thing you want to do this to, or might it sometimes be something curved [e.g. an Arc] or even closed [e.g. a Circle]?  Etc.

Kent Cooper, AIA

paullimapa
Mentor
Mentor

What if you created a BLOCK called 2M that has a LINE segment = 0,2M with insert point as Midpoint of the LINE at same orientation as the LINE you want to DIVIDE?

Then use AutoCAD's built-in DIVIDE command, choose the LINE and specify Block 2M, Align with object, then enter 10 as number of segments. AutoCAD will DIVIDE the LINE using the 0,2M Block and you can now osnap to the end points of the 0,2M line segment to dimension.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos

Kent1Cooper
Consultant
Consultant

@paullimapa wrote:

What if you created a BLOCK called 2M that has a LINE segment = 0,2M with insert point as Midpoint of the LINE at same orientation as the LINE you want to DIVIDE?

Then use AutoCAD's built-in DIVIDE command. ....


That will leave the end segments longer than the intermediate ones, for example:

Kent1Cooper_0-1663171462654.png

 

 

Kent Cooper, AIA
0 Likes

Kent1Cooper
Consultant
Consultant
Accepted solution

In simplest terms, for any finite object with linearity, open or closed, to place Point objects along it at the ends of the "gaps" [but without causing actual gaps]:

 

(vl-load-com)
(defun C:SWG ; = Subdivide With Gaps
  (/ ent gap segs len efflen gaps seg dst)
  (setq
    ent (car (entsel "\nObject to Subdivide With Gaps: "))
    gap (getdist "\nGap width: ")
    segs (getint "\nNumber of subdivisions: ")
    len (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent))
    efflen (- len (* gap (setq gaps (- segs (if (vlax-curve-isClosed ent) 0 1)))))
    seg (/ efflen segs)
    dst 0
  )
  (setvar 'pdmode 35)
  (repeat gaps
    (command
      "_.point" "_non" (vlax-curve-getPointAtDist ent (setq dst (+ dst seg)))
      "_.point" "_non" (vlax-curve-getPointAtDist ent (setq dst (+ dst gap)))
    )
  )
  (princ)
)

 

It does this kind of thing:

Kent1Cooper_0-1663172833091.png

It could be made to:
1.  Break the object instead of or in addition to placing Points;

2.  Verify that what you picked is appropriate [no Rays, Xlines, Text, Blocks, etc.];

3.  Remember your choices of gap size and number of segments, to offer as defaults the next time;

4.  Save the PDMODE setting when you start, and restore it afterwards;

5.  Allow selection of multiple objects to process all in the same way;

6.  Wrap its operations in Undo Begin/End so Undo doesn't remove just one Point at a time;

7.  Put the Points on a specified Layer [and make it if it doesn't exist], or on the Layer of the object;

8.  Place other kinds of things, such as Blocks, little perpendicular Lines that you can Trim between, etc.;

9.  Have an *error* handler to ensure resetting of anything that was changed.

Kent Cooper, AIA

rafaelcmeyer
Explorer
Explorer

Hi, @Kent1Cooper,

 

Thank you very much for your quick response. Yes, the image that I posted was not a line, but this is exactly what I was looking for. I never coded anything in my life, so many thanks for providing the AutoLisp routine too.

 

@paullimapa, thanks for your contribution too.

0 Likes

paullimapa
Mentor
Mentor

So I would typically extend both ends of the object in this case a LINE half distance of the gap and then DIVIDE.

But of course your custom code offers a lot more advantages.

paulli_apa_0-1663181975783.png

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

saqib_tipa
Advocate
Advocate

 

Hi @Kent1Cooper,

Can you please make this lisp so that it should deduct gap length from beginning and end of line and apply function on remaining part.

Suppose length of line is 10 units, we enter gap value 1 unit then it should deduct 1 from start and 1 from end of line and apply function in remaining part which will be 8.
Thank you.

 

 


@Kent1Cooper wrote:

 

 

(vl-load-com)
(defun C:SWG ; = Subdivide With Gaps
  (/ ent gap segs len efflen gaps seg dst)
  (setq
    ent (car (entsel "\nObject to Subdivide With Gaps: "))
    gap (getdist "\nGap width: ")
    segs (getint "\nNumber of subdivisions: ")
    len (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent))
    efflen (- len (* gap (setq gaps (- segs (if (vlax-curve-isClosed ent) 0 1)))))
    seg (/ efflen segs)
    dst 0
  )
  (setvar 'pdmode 35)
  (repeat gaps
    (command
      "_.point" "_non" (vlax-curve-getPointAtDist ent (setq dst (+ dst seg)))
      "_.point" "_non" (vlax-curve-getPointAtDist ent (setq dst (+ dst gap)))
    )
  )
  (princ)
)

 

 

 


 

0 Likes

Kent1Cooper
Consultant
Consultant

@saqib_tipa wrote:

... make this lisp so that it should deduct gap length from beginning and end of line and apply function on remaining part.

....


If I understand correctly, still placing Points only but not changing the selected object itself, I think this does what you're asking [minimally tested]:

(vl-load-com)
(defun C:SWG ; = Subdivide With Gaps
  (/ ent gap segs len efflen gaps seg dst)
  (setq
    ent (car (entsel "\nObject to Subdivide With Gaps: "))
    gap (getdist "\nGap width: ")
    segs (getint "\nNumber of subdivisions: ")
    len (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent))
    efflen (- len (* gap (+ (setq gaps (- segs (if (vlax-curve-isClosed ent) 0 1))) 2)))
    seg (/ efflen segs)
    dst 0
  )
  (setvar 'pdmode 35)
  (repeat gaps
    (command
      "_.point" "_non" (vlax-curve-getPointAtDist ent (+ gap (setq dst (+ dst seg))))
      "_.point" "_non" (vlax-curve-getPointAtDist ent (+ gap (setq dst (+ dst gap))))
    )
  )
  (princ)
)

It just takes two more gaps' worth off the effective length to work with, and places each Point one gap's worth farther along the object than in the original.

Kent Cooper, AIA

saqib_tipa
Advocate
Advocate

 

@Kent1Cooper 

 

Thank you for the reply. Yes, this is what I want.

 

Gap which it makes in the start and in the end of line these two points are also required. How to place these two points also.

 

Thank you.

0 Likes

Sea-Haven
Mentor
Mentor

This is gapping in two directions example, can you post a sample dwg so can see what is required a before and after. 

 

 

0 Likes

saqib_tipa
Advocate
Advocate

 

@Sea-Haven 

where is the code.

Thank you.

0 Likes

Kent1Cooper
Consultant
Consultant

@saqib_tipa wrote:

....Gap which it makes in the start and in the end of line these two points are also required. ....


Try adding this [untested]:

....

   (setvar 'pdmode 35)
  (command
    "_.point" "_non" (vlax-curve-getPointAtDist ent gap)
    "_.point" "_non" (vlax-curve-getPointAtDist ent (- len gap))
  ); command
   (repeat gaps

....

Kent Cooper, AIA

saqib_tipa
Advocate
Advocate

 

@Kent1Cooper 

 

Yes, this is working. It is now adding those two points also.

 

Thank you so much for your help.

 

 


Try adding this [untested]:

....

   (setvar 'pdmode 35)
  (command
    "_.point" "_non" (vlax-curve-getPointAtDist ent gap)
    "_.point" "_non" (vlax-curve-getPointAtDist ent (- len gap))
  ); command
   (repeat gaps

....


 

0 Likes