I need to create an identical hatch to a given one (using C#). In the real case this is done in a different process on a different computer so I can't use clipboard, copy commands or such - I need to read the object's properties and recreate a new instance based on those.
I already posted a related question here:
http://www.theswamp.org/index.php?topic=50188.0 (link provided for reference - no need to read through that, I'll make this post self explanatory).
With the kind help from theswamp users I got the access violation problem solved and everything seemed to work. Now after some testing it turns out that the code only works on associative hatches that have a single object as their boundary. If the associative hatch is bounded by several objects the call to AppendLoop causes eInvalidInput (see the attached code, lines 82-87):
// option 1: add the hatch loop as it is in the original hatch.
// Problem: call to AppendLoop throws eInvalidInput
using ( var boundaryColl = new ObjectIdCollection( associatedObjectIds ) ) {
h.AppendLoop( ltype, boundaryColl );
}
I also tried adding all the boundaries as separate loops (see the attached code, lines 89-99):
// option 2: add each boundary object as a separate loop
// ( as in the second code sample in http://www.theswamp.org/index.php?topic=20832.msg253807#msg253807 )
// Problem: the hatch is created in the wrong place
using ( var boundaryColl = new ObjectIdCollection() ) {
foreach ( var oid in associatedObjectIds ) {
boundaryColl.Clear();
boundaryColl.Add( oid );
h.AppendLoop( ltype, boundaryColl );
}
}
The problem with this approach is that the hatch is created in the wrong intersection of the given objects.
Attached is a drawing and code to easily reproduce the problem. Here's what to do:
* comment out either option 1 or option 2 in the source code provided (you can find them easily by searching for e.g. "option 1") depending on which of the given problems you wish to reproduce
* compile the given source code and load it to AutoCAD to make the commands defined therein to be available
* open the attached drawing (hatch_with_circular_boundary.dwg)
* select the hatch inside the rectangle that does not intersect with any other object and run command NSASSOCHATCH (that invokes the provided code). This works fine with both option 1 and option 2 code
as there is only a single boundary object
* now select the hatch in the intersection of the circle and and a rectangle
* using code in option 1 results in eInvalidInput thrown from AppendLoop
* using code in option 2 results in the new hatch being created in the wrong intersection of the circle and the square
Any ideas on how to successfully recreate an associative hatch with several boundary objects using C# are highly appreciated.
My environment: AutoCAD 2015 64-bit on Windows 7