Jump to content

Minimal distance between different buildings on an AUTOCAD plan


Recommended Posts

Posted

Hello,

 

I am new to autolisp and I would like to develop an autolisp program that allows the user to select a building on a plan and automatically obtain all the distances between the selected building and the others (minimum distances), so if there are 100 buildings that at the end of the program the 99 distances are displayed and indicate the names of the corresponding buildings.

 

My autocad file is a plan containing a layer with the buildings outlines and other layers that are not relevant to the problem.

 

My hard points are as follows:
- The building layouts are only in one layer, no blocks, polylines, nothing...
- the distance command can only be used between two points, there is no equivalent (to my knowledge) between two objects.

 

I saw that the following code could be used to obtain the minimum distance between two polylines: https://autocadtips1.com/2017/03/13/lisp-version-of-closest-distance-between-two-objects/. One problem with this code is that it doesn't take any arguments, so I'll have to modify it to use it and avoid user inputs (or maybe i can call a subfunction and specify what to do whan user inputs are necessary?)

 

I thought I'd get away with creating polylines for each building and creating a block for each building (long but one time task) so to I can obtain the name of the building.

So in the end, ask the user to select the building of their choice, loop over all the other blocks, use the code found to obtain the distance (you'll have to deal with the lack of arguments for this to be automatic) and get the name of the building with each block.

 

Do you think, it's feasible?
Do you have any alternative that might be simpler?

 

Thanks

Posted

There is a function that calculates the distance from a point to  an object.

So I'd say make each house 1 closed polyline (for example the outline).

 

Then, the user selects 1 house.  The point at which you select the house is the basis point for the distances to the other houses.

It's not exactly what you ask for.  It's a start.

 

(defun c:test ( / ss i sel pt ptt ent ent2 pt)
	
	(setq ent (car (setq sel (entsel "\nSelect house: "))))
	(setq pt (cadr sel))
	(setq lay (cdr (assoc 8 (entget ent))))
	(setq ss (ssget "_x" (list (cons 8 lay))))
	
	(setq i 0)
	(repeat (sslength ss)
		(setq ent2 (ssname ss i))
		(setq ptt (vlax-curve-getclosestpointto (vlax-ename->vla-object ent2) pt))
		(setq dst (distance pt ptt))
		
		(princ "\n")
		(princ i)
		(princ ": ")
		(princ dst)
		(setq i (+ i 1))
	)
	(princ)
)

 

distance_between_houses.dwg

Posted

Ok thank you that's interesting! I'll look into it

 

Do you think it's worth basing the program on the code I've found, or would it be too complicated?

Posted

You talk about 100 houses the issue is how do you determine the search order of the houses, a say 4 point house will give at least 4 answers based on the corners. Not sure that you can automate but can speed up. Like @Emmanuel Delay you may need to pick the segments to be compared. I am sure this task was asked recently remember posting something. Now the hard part find it again.

 

Your lucky found this.

; dim between plines
; BY AlanH Nov 2024

(defun c:lotdim ( / ent obj pt1 pt2 oldsnap)
  (setq oldsnap (getvar 'osmode))
  (while (setq ent (car (entsel "\nPick bounding object Enter to stop ")))
   (setq obj (vlax-ename->vla-object ent))
   (setvar 'osmode 1)
   (while (setq pt1 (getpoint "\nPick point on building Enter to stop "))
    (setq pt2 (vlax-curve-getClosestPointTo obj pt1))
    (command "dimaligned" "_non" pt1 "_non" pt2 "_non" pt2)
   )
  (setvar 'osmode oldsnap)
  )
  (princ)
)
(c:lotdim)

 

Posted

Thank you for your response.

 

One of the problems is that, because there are so many buildings, you have to zoom in to select the nearest points if you're going for a more manual approach. At first, I thought I'd enter the coordinates of each building, then use the zoom command and ask the user to choose the nearest corresponding corner (but it is still long and you do not always know which corner to chose). I thought that a totally automatic method was possible

 

That's why I wanted to use the code found here : https://autocadtips1.com/2017/03/13/lisp-version-of-closest-distance-between-two-objects/

You only need to select two polylines for instance and you automatically get the minimal distance, you only need to select manually the two objects.

 

Posted (edited)

So does the code you have now work ? 

 

Ok so if you remove last line in my code (c:lotdim) instead load the lisp, then type lotdim you pick object 1 then pick points, press enter 3 times and that is do an entire new go at dimensions, its reasonably quick once you get the hang of it.

 

image.png.51b5b6e3541a77c35f6c34e6cdc6ad23.png

Edited by BIGAL
Posted
11 hours ago, BIGAL said:

So does the code you have now work ? 

 

Ok so if you remove last line in my code (c:lotdim) instead load the lisp, then type lotdim you pick object 1 then pick points, press enter 3 times and that is do an entire new go at dimensions, its reasonably quick once you get the hang of it.

 

image.png.51b5b6e3541a77c35f6c34e6cdc6ad23.png

 

 

Yes, it works perfectly!
I've just changed the code a little because I don't want to display the aligned dimensions, just the distances. 

I'm still trying to make it fully automatic by using the code I found above.

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...