Are you looking for a way to attach or detach dwg files in AutoCAD Map 3D using .NET API?
If the answer is ‘YES’, you are at the right place. DrawingSet class in Map 3D .NET API has the necessary API function to attach a DWG file using DrawingSet.AttachDrawing() as well as detach a DWG file using DrawingSet.DetachDrawing(). To access the DrawingSet object you need to make a reference to the ManagedMapApi assembly located in the Map 3D installation folder (For Map 3D 2012 release its here - C:\Program Files\Autodesk\AutoCAD Map 3D 2012\ManagedMapApi.dll)
Here is the code snippet which demonstrates the Attach and Detach DWG files using Map 3D .NET API functions –
public void AttachDWG()
{
MapApplication mapApp = HostMapApplicationServices.Application;
ProjectModel activeProject = mapApp.ActiveProject;
ProjectCollection progList = mapApp.Projects;
DrawingSet dwgSet = activeProject.DrawingSet;
// String to indicate the DWG file path
string dwgStr = @"C:\Data_set\DWGs_Map\Street_Centerlines.dwg";
try
{
AttachedDrawing dwgAttached = dwgSet.AttachDrawing(dwgStr);
dwgAttached.Preview();
}
catch (MapException ex)
{
ed.WriteMessage(ex.Message);
}
}
public void DetachDWG()
{
MapApplication mapApp = HostMapApplicationServices.Application;
ProjectModel activeProject = mapApp.ActiveProject;
ProjectCollection progList = mapApp.Projects;
DrawingSet dwgSet = activeProject.DrawingSet;
try
{
// Check how many DWG files are attached and then Detach each of them
while (dwgSet.DirectDrawingsCount > 0)
{
dwgSet.DetachDrawing(0);
ed.WriteMessage("attached number: " + dwgSet.DirectDrawingsCount.ToString() + "\n");
}
}
catch (MapException ex)
{
ed.WriteMessage(ex.Message);
}
}
To know more about the AutoCAD Map 3D .NET API, refer to the Map 3D SDK documents and the API samples in the SDK.