Message 1 of 5
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I am trying to create a routine for inserting a block (ie. Record Stamp) into multiple dwg files. I thought I have all the information but I can not get the routine to run. I have tried getting the documents and adding a lock and it still does not seem to make a difference. Is there something that i am missing in my code to do this, or is this even possible?
Thanks in Advance
private static Document _document;
private static Editor _commandLine;
[CommandMethod("gcs-MultiDocInsertBlock")]
public void MultiDocInsertBlock()
{
_document = CADApplication.DocumentManager.MdiActiveDocument;
_commandLine = _document.Editor;
BlockFileDialog().ShowDialog();
var blockFileName = BlockFileDialog().FileName;
var blockName = Path.GetFileName(blockFileName);
if (FileDialog().ShowDialog() != DialogResult.OK)
return;
var insPt = SetPoint();
foreach (var file in FileDialog().FileNames)
{
_commandLine.WriteMessage("file is "+ file);
try
{
using (var database = new Database(false, true))
{
Document document = CADApplication.DocumentManager.GetDocument(database);
using(document.LockDocument())
{
database.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, true, null);
using (var transaction = database.TransactionManager.StartOpenCloseTransaction())
{
var blockTable = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
if (blockTable.Has(blockName))
{
_commandLine.WriteMessage("block does exist!");
return;
}
_commandLine.WriteMessage("block does not exist!");
using (var sourceDb = new Database(false, true))
{
sourceDb.ReadDwgFile(blockFileName, FileOpenMode.OpenForReadAndAllShare, true, "");
database.Insert(blockName, sourceDb, true);
}
using (var blockReference = new BlockReference(insPt, blockTable[blockName]))
{
var space = (BlockTableRecord)transaction.GetObject(database.CurrentSpaceId,
OpenMode.ForWrite);
space.AppendEntity(blockReference);
transaction.AddNewlyCreatedDBObject(blockReference, true);
}
transaction.Commit();
}
database.SaveAs(file, DwgVersion.Current);
}
}
}
catch (Exception ex)
{
_commandLine.WriteMessage($"\n{file}: {ex}");
}
}
}
private static Point3d SetPoint()
{
var promptPointOptions = new PromptPointOptions("")
{
Message = "\nEnter the start point of the line: "
};
var promptPointResult = _commandLine.GetPoint(promptPointOptions);
var point3d = promptPointResult.Value;
return point3d;
}
private static OpenFileDialog FileDialog()
{
var openFileDialog = new OpenFileDialog
{
InitialDirectory = "c:\\",
Filter = @"DWG Files (*.dwg)|*.dwg",
FilterIndex = 2,
RestoreDirectory = true,
Multiselect = true,
Title = @"Select CAD files to update"
};
return openFileDialog;
}
private static OpenFileDialog BlockFileDialog()
{
var openFileDialog = new OpenFileDialog
{
InitialDirectory = "c:\\",
Filter = @"DWG Files (*.dwg)|*.dwg",
FilterIndex = 2,
RestoreDirectory = true,
Multiselect = false,
Title = @"Select block file to insert"
};
return openFileDialog;
}
Solved! Go to Solution.