Hello Everyone,
I am trying to create a standalone exe project which removes some unwanted layers from a AutoCAD drawing.
I am using Visual Studio 2010 with .NET Framework4 and C#.NET as programming language.
I have AutoCAD 2014 installed on my machine.
When i try to run the following code, I get the exception - "Object is referenced by other object(s)"
Autodesk.AutoCAD.Interop.AcadApplication acadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.19.1");
Autodesk.AutoCAD.Interop.AcadDocument acadDoc = acadApp.Documents.Open("AutoCAD Drawing File Path", false);
foreach (Autodesk.AutoCAD.Interop.Common.AcadLayer Layer in acadDoc.Layers)
{
string sLayerName = Layer.Name;
// check the layer is not among the layers that we want to keep in the drawing
IEnumerable<DataRow> drLayer = from layer in dtLayers.AsEnumerable()
where layer.Field<string>("Layer").Trim() == sLayerName
select layer;
if (drLayer.Count<DataRow>() == 0)
{
//Layer.LayerOn = false; // works fine
Layer.Delete(); // throws exception
acadApp.Application.Update();
}
}
Please tell me the solution for this problem.
Thanks,
Kaustubh Devasthali.