A simple DWG containing a simple closed pline when converted to an AecGeRing, reports that the ring is not Valid. Why? The pline looks perfectly fine!
Even though to a polyline might look fine, there might be some disjoin somewhere which might be causing this. To avoid this unwanted behavior, we can use the JoinColinearSegments() method to fix any such disjoin problems. After using this method, the same polyline now returns IsValidRing value to be True. Here is the complete code which shows how use this API (which includes code to convert a polyline to a Ring):
Editor mEditor;
Database mDb;
Autodesk.AutoCAD.DatabaseServices.TransactionManager mTransMgr;
Transaction mTrans;
[CommandMethod("Ring", Autodesk.AutoCAD.Runtime.CommandFlags.Modal)]
public void Ring()
{
mEditor =
AcadApp.DocumentManager.MdiActiveDocument.Editor;
mDb =
AcadApp.DocumentManager.MdiActiveDocument.Database;
mTransMgr =
AcadApp.DocumentManager.MdiActiveDocument.TransactionManager;
// start the transaction
mTrans = mTransMgr.StartTransaction();
try
{
PromptEntityOptions prOptions =
new PromptEntityOptions("\nSelect polyline to modify: ");
prOptions.SetRejectMessage("\nObject must be of type Polyline.");
prOptions.AddAllowedClass(
typeof(Autodesk.AutoCAD.DatabaseServices.Polyline), true);
// prompt the user to select a polyline
PromptEntityResult prResult = mEditor.GetEntity(prOptions);
// get the ObjectId from the selected polyline
Autodesk.AutoCAD.DatabaseServices.ObjectId oidSelected =
prResult.ObjectId;
// open the polyline for Write based on the objectId
Autodesk.AutoCAD.DatabaseServices.Polyline polyline =
(Autodesk.AutoCAD.DatabaseServices.Polyline)mTransMgr.GetObject(
oidSelected, OpenMode.ForWrite, false);
Autodesk.Aec.Geometry.Ring ring = new Autodesk.Aec.Geometry.Ring();
ring.SubSetDatabaseDefaults(mDb);
ring.SetToStandard(mDb);
ring.AddSegmentsFrom(polyline, true);
ring.JoinColinearSegments();
mEditor.WriteMessage(
"\nIs a Valid Ring: "
+ ring.IsValidRing.ToString());
mEditor.WriteMessage(
"\nIs a Self Intersecting: "
+ ring.IsSelfIntersecting.ToString());
}
catch (System.Exception ex)
{
mEditor.WriteMessage(ex.Message);
}
finally
{
// Commit the transaction
mTrans.Commit();
}
}
And here is the output:
Command: ring
Select polyline to modify:
Is a Valid Ring: True
Is a Self Intersecting: False