Hi Will,
Here is a bad example of what I think your asking.
I threw this together last week because we receive drawing from architects and they constantly send updates and it might just be a door swing changes or a wall moves, etc.....
Anyways a guy here opens each drawing received explodes everything because he does not understand how to change the color the block if the definition contains entities where the color is set by a layer other than zero or explicitly set, and changes every layer color to what he already has set 30 times. Also another guy copies the new drawing and paste it next to the old drawing and tries to eyeball what the changes are.
So I gave them some code to change all entities in blocks to layer zero and set ByLayer, and the code below which basic idea is to delete everything in model keeping all block and layer settings, etc... and WBlocking all the entities in from the new drawing. Did it for other reasons and tried to explain to them to freeze layers instead of deleting them because the archs usually already have closed polylines used for getting area that can be reused for automating creating ceiling grid for lights, etc......
Anyways just slapped this together to keep from slapping them and until I have time come up with something better or different workflow, but passes a string for filename by means of SendStringToExecute and Editor.GetString().
The main reason it is broken up into two methods is because it would crash and did not have time to figure out what was going on but a drawing with dynamic blocks seemed to crash every time.
[CommandMethod("UpdateFromNewDrawing")]
public void UpdateFromNewDrawing()
{
OpenFileDialog ofd
= new OpenFileDialog
("New File", Path
.GetFileName(Db
.Filename),
"dwg",
"New File",
0);
if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
return;
}
string fileName = ofd.Filename;
ObjectId targetId;
using (Transaction trx = Db.TransactionManager.StartTransaction())
{
BlockTableRecord modelSpace = Db.ModelSpace();
targetId = modelSpace.ObjectId;
foreach (Entity ent in modelSpace.GetEntities(OpenMode.ForWrite, false, true))
{
ent.Erase();
}
trx.Commit();
}
Doc.SendStringToExecute("*_WblockCloneObjectsIntoCurrentDrawing " + fileName + "\n", false, false, false);
}
[CommandMethod("*_WblockCloneObjectsIntoCurrentDrawing", CommandFlags.NoHistory)]
public void __WblockCloneObjectsIntoCurrentDrawing()
{
PromptStringOptions pso
= new PromptStringOptions
("\nEnter File Name"); pso.AllowSpaces = true;
PromptResult pr = Ed.GetString(pso);
if (pr.Status != PromptStatus.OK)
{
return;
}
string fileName = pr.StringResult;
if (File.Exists(fileName))
{
PromptKeywordOptions pko
= new PromptKeywordOptions
("\nUpdate Blocks"); pko.Keywords.Add("Yes");
pko.Keywords.Add("No");
pko.Keywords.Default = "Yes";
pr = Ed.GetKeywords(pko);
if (pr.Status != PromptStatus.OK)
{
return;
}
DuplicateRecordCloning dupRecordCloning = pr.StringResult == "Yes" ? DuplicateRecordCloning.Replace : DuplicateRecordCloning.Ignore;
ObjectId targetId;
using (Transaction trx = Db.TransactionManager.StartTransaction())
{
BlockTableRecord modelSpace = Db.ModelSpace();
targetId = modelSpace.ObjectId;
trx.Commit();
}
using (Database sideDb
= new Database
(false,
true)) {
ObjectIdCollection ids;
sideDb.ReadDwgFile(fileName, System.IO.FileShare.ReadWrite, true, "");
sideDb.CloseInput(true);
using (Transaction sideTrx = sideDb.TransactionManager.StartTransaction())
{
BlockTableRecord sideModel = sideDb.ModelSpace();
ids
= new ObjectIdCollection
(sideModel
.GetObjectIds().ToArray()); if (ids.Count > 0)
{
IdMapping idMap
= new IdMapping
(); sideDb.WblockCloneObjects(ids, targetId, idMap, dupRecordCloning, false);
}
sideTrx.Commit();
}
}
}
else
{
Ed.WriteLine(fileName + " Not found!!");
}
}