- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Motivation:
I need to side load the AutoCAD database as per below, because otherwise I won't be able to run the unit tests. I wrote the below code modelling after @CADbloke 's excellent code.
Problem statement:
For some reason, if I side load the AutoCAD database, the CivilApplication.ActiveDocument won't be populated accordingly. Here's the code:
[CommandMethod(nameof(SideLoadingDatabase))]
public void SideLoadingDatabase()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
using (doc.LockDocument())
{
using (var db = new Database(false, false))
{
var drawingPath = @"D:\.net Samples project\September 2022\AcadTestBug\AcadTestBug\Drawing1.dwg";
db.ReadDwgFile(drawingPath, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
using (new WorkingDatabaseSwitcher(db))
{
using (var ts = db.TransactionManager.StartTransaction())
{
var sourceBlockTable = ts.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
var sourceModelSpace =
ts.GetObject(sourceBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
var civilDoc =CivilDocument.GetCivilDocument(db); //this still doesn't change anything
var objectIdCollection = civilDoc.GetSurfaceIds(); //this is 0 count even though the DWG does contain surface
var allNetwork = civilDoc.GetPipeNetworkIds(); //this is 0 count even though the DWG does contain Pipe networks
foreach (var objeId in sourceModelSpace)
{
var tinSurface = ts.GetObject(objeId, OpenMode.ForWrite) as TinSurface;
if (tinSurface != null)
{
// Yes, TinSurface exists ! And so does Pipe Network!!
}
}
ts.Commit();
}
}
}
}
}
public sealed class WorkingDatabaseSwitcher : IDisposable
{
Database _oldDb = null;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="db">Target database.</param>
public WorkingDatabaseSwitcher(Database db)
{
_oldDb = HostApplicationServices.WorkingDatabase;
HostApplicationServices.WorkingDatabase = db;
}
public void Dispose()
{
HostApplicationServices.WorkingDatabase = _oldDb;
}
}
No such problem if I just open the DWG drawing in the Civil 3D and then run the above command line.
As mentioned in the code, even though Civil objects like TinSurface and PipeNetwork exist in the drawing, but civilDoc.GetSurfaceIds() and civilDoc.GetPipeNetworkIds() return 0 count.
How can I get CivilApplication.Document working correctly if I have to sideload the database?
Ngu Soon Hui
##########
I'm the Benevolent Dictator for Life for MiTS Software. Read more here
I also setup Civil WHIZ in order to share what I learnt about Civil 3D
Solved! Go to Solution.