Hi Jeff,
see if you discover any similarities:
Sorry Thorsten I do not understand the question
I tried to reduce his function to the bare minmum, that is:
Here is another idea I hope this helps
This is Fixo's code that obtains the entities ObjectId a different way.
1. It uses a dictionary and does one loop through model space adding each entity to the dictionary
2. Tries to create new drawing for each layer from the template "Fart.dwt" ---- Did this so it would fail and use default template
3. Save in same folder as layer Name
Not sure if this will help reduce the function a little more or is not efficient enough.
Here is the Code
************Edited************
Added a couple of comments in the code
************Edited************
using System;
using System.Collections;
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
[assembly: CommandClass(typeof(WblockLayers.MyCommands))]
namespace WblockLayers
{
public class MyCommands
{
// Dictionary to Store the Layer Name and the Entites that are on the Layer
Dictionary<string, ObjectIdCollection> entFileDict = new Dictionary<string, ObjectIdCollection>();
[CommandMethod("WblockLayers")]
public void WblockLayers()
{
// Get the folder that the current drawing is in
string dwgDialog = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("DWGPREFIX") as string;
Document doc = Application.DocumentManager.MdiActiveDocument;
DocumentCollection docMgr = Application.DocumentManager;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction trx = db.TransactionManager.StartTransaction())
{
BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord btrMs = bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForRead) as BlockTableRecord;
ObjectIdCollection entIds = null;
// For each entity in Model Space it checks if the Dictiony contains the layer name
// If it does it adds the Entities Object Id to the value
//If not it creates a new entry
foreach (ObjectId entID in btrMs)
{
Entity ent = entID.GetObject(OpenMode.ForRead) as Entity;
string lyrName = ent.Layer;
if (entFileDict.TryGetValue(lyrName, out entIds))
{
entIds.Add(entID);
}
else
{
ObjectIdCollection entObjIds = new ObjectIdCollection();
entObjIds.Add(entID);
entFileDict.Add(lyrName, entObjIds);
}
}
// Loop through the dictionary using the key values which are the layer names
// Create new drawing add the entites on that layer
// Save drawing in current drawing folder as the layer name and close drawing
foreach (string lyrName in entFileDict.Keys)
{
using (Document newDoc = docMgr.Add("Fart.dwt"))
{
using (DocumentLock newLoc = newDoc.LockDocument())
{
using (Transaction newTrx = newDoc.Database.TransactionManager.StartTransaction())
{
BlockTable newBt = newDoc.Database.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord newBtrMs = newBt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForRead) as BlockTableRecord;
IdMapping map = new IdMapping();
ObjectIdCollection newEntIds = null;
if (entFileDict.TryGetValue(lyrName, out newEntIds))
{
db.WblockCloneObjects(newEntIds, newBtrMs.Id, map, DuplicateRecordCloning.Replace, false);
}
else
{
continue;
}
newTrx.Commit();
newDoc.Database.SaveAs(dwgDialog + lyrName + ".dwg", DwgVersion.Current);
}
}
newDoc.CloseAndDiscard();
}
}
trx.Commit();
}
}
}
}
Thanks Fixo for the cool idea and code.
Thanks Thorsten for the F# code.