Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to get the shared boundary segment length between two Hatch objects?

Message 1 of 14
651 Views, 13 Replies

How to get the shared boundary segment length between two Hatch objects?

Hi guys,


I have two hatch objects like these:



these objects share (or touch) eachother boundaries just in one small part as we can see here:

The challenge I am facing right now is that I need to know the length of that 'shared' segment, I am just starting with the .Net API and cant find any approach that can lead me to that information.


I attached the sample file.


Do any of you face a similar problem or have a basic idea on how to tackle this situation?


Thanks a lot in advance for your support.


Best Regards

Tags (2)
Labels (1)
Message 2 of 14
in reply to: al188195

Assuming that no two hatch boundaries intersect (have a common area), then:


let 'a' = the sum of the perimeters of all the hatch boundaries.

let 'b' = the perimeter of the Boolean Union of all the hatch boundaries.

The length of all common boundary segments is (a-b)/2

Message 3 of 14
in reply to: ActivistInvestor

Hi @ActivistInvestor ,


first of all thanks for replying, it helped me to move forward in the right direction, the approach you suggested works for a set of scenarios, worth to mention that I have hatch objects but in order to use the BooleanOperation I needed to manually (using the editor) create the corresponding regions, then I can perform the boolean operation and apply your logic, after that it correctly provides the length as we can see next:


a. Regions created out of hatch objects using the editor

a. Regions created out of hatch objects using the editora. Regions created out of hatch objects using the editor


b. After the boolean operation we get a united region as we can see here:



c. Finally from your logic we can deduce the shared boundary length ant its OK as we can see here:



So far so good, now based on your feedback I have a couple of additional doubts:


1.- Is there a way to create the Region object directly from the Hatch object, I tried to use the hatch object and pass it to a DBObjectCollection and then use Region.CreateFromCurves but I keep getting the eInvalidInput, for my testing as explained before I had to use the REGION command from the editor applying it to the polilynes that belongs to my hatch objects.


Hatch mainHatch = transaction.GetObject(selectedObject.ObjectId, OpenMode.ForWrite) as Hatch

DBObjectCollection acDBObjColl = new DBObjectCollection();
acDBObjColl.Add(mainHatch );
// Calculate the regions based on each closed loop
DBObjectCollection myRegionColl = new DBObjectCollection();
myRegionColl = Region.CreateFromCurves(acDBObjColl);


2.- There is a chance that the two hatch objects involved at some point might not be really overlapping in other words they might no be sharing the boundary at any point, event when they are intended to, for example if the person working on the drawing does not pay attention to it it might well be separated and it would only be visible when you zoom in enough, so the question is : Is it possible to identify this difference (slopy drawing) and use some sort of treshold to identify it as if it is shared?


I really appreciate your time to answer initially, any additional feedback you may provide would be amazing!



Message 4 of 14
in reply to: al188195





1- POLYLINE .Explode


3-  GET  DistanceTo POLYLINE 2   = 0  OR  <0.1








Message 5 of 14
in reply to: al188195

1. Get hatch boundary => pline1, pline2

pline1 = hatch1.GetLoopAt(0).Curves[0] as Polyline; 

2. Get all segments of polyline to List<Polyline> => ls1, ls2

for (int i=0; i< pline1.NumberOfVertices; i++) { ls1.Add (new Polyline ...)}

3. bool method compare two pline segments 

4. Check share segments in ls1, ls2 => ls3

5. Sum length all segments in ls3.

Message 6 of 14
in reply to: al188195

CreateFromCurves() wants a DBObectCollection that contains instances of the Curve class (e.g. Line, Arc, Polyline, etc.).


It's a bit more complicated, see this post:



Message 7 of 14
in reply to: al188195

A less complicated method of getting the Region representing the boundary of a Hatch would be to P/Invoke the native AcDbHatch::getRegionArea() function.

Message 8 of 14
in reply to: ActivistInvestor

Thanks for the response, indeed the method you proposed works to identify the shared boundary length, thanks for that, I am able to create regions from hatch objects with the feedback you provided as well so I can perform the boolean union operation as described in your algorithm, now that approach will work if and only if the two regions are actually sharing the boundary but if for some reason they are not sharing this segment of the boundary it will not work because then the algorithm will pretty much return -3.xxx e -15 so an incredible small number because there is no sharing segment, unfortunately there is a chance that this scenario to be more often than not, here some images to showcase this scenario:


without zoom we can see that it looks like there is a sharing boundary piece as originaly stated




but as soon as we zoom in we can see the gap between the two regions



I added some annotation to display the separation but as mentioned before is really small so I get 0.00



so do you happen to know how to tackle this new challenge, the boolean operation will unite both regions but then the perimeter data is not able to identify the differenece because since the regions are not realy shring boundaries it will not be a united perimeter.


Again any idea in the right direction is highly appreciated.


Thanks a lot again!

Message 9 of 14
in reply to: cuongtk2

Hi @cuongtk2 

Thanks for replying, I am currently testing your approach, I will let you know the result.



Message 10 of 14
in reply to: al188195

I'm afraid that any solution that deals with inaccuracy resulting from 'sloppy drafting' or various other causes is non-trivial. The solution I suggested leverages AutoCAD's modeler and Boolean operations to solve the problem efficiently and without the need for a lot of analytical code.

Message 11 of 14
in reply to: ActivistInvestor

Fair enough, in this specific case I do need to look for the more realistic scenarios where my design department might have this kind of 'slopy drafting' and manage them.

Again thanks a lot for your time!
Message 12 of 14
in reply to: al188195

@al188195 , hi

excuse me for being pedantic ; 

In your first post you stated:

>>>> these objects share (or touch) eachother boundaries just in one small part as we can see here:

. . . The challenge I am facing right now is that I need to know the length of that 'shared' segment, . . .  


 Changing the concept and from 'shared length' to 'near edge' is considerable.
You will probably need to identify and isolate the adjacent hatch boundary segments and determine their relationship , then decide on the action you want to take.
We don't know the real final purpose of your endevours
  or how many cases you will apply your solution to

  or what the hatching represents
  or the lifespan of the program.

I'm currently wondering if the program will even be cost effective.
From my past time as a drafter I have a good idea of the time I'd take to review manually the drawing you posted.

I'm not belittling your endevours, just concerned about your return on investment in a situation where you have no control over the data and where the decisions about the situation are myriad.









// Called Kerry in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 13 of 14
in reply to: kdub_nz

@kdub_nz , no worries its not the first time I get replies like this, I totally agree there is a significant difference between the original question and the latest, it was more kind of continuation on the discussion with the people that replied to the first question, sorry my bad.





Message 14 of 14
in reply to: hosneyalaa

Hi @hosneyalaa.,


Thanks for replying, I can see from the video you kindly shared that you actually able to identify the specific segments that are being shared between both polylines, to be honest the tool you are using by connecting node and all of that looks pretty cool, but its not clear to me how to achieve that using net api code and the segments from both polylines, probably not that relevant but I am not an Autocad Dev, I have good amount of experience developing software with .Net but with the AutoCad NET API this is the first time I face it so, I am just learning it.


it would be awesome if you could share the basic steps to follow in order to identify that shared segment using net code, probably a quick description of what each of the nodes you are using in your tool is doing, could help me to find the specific object.


In any case again thanks for sharing!



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

Post to forums  

Technology Administrators

Autodesk Design & Make Report