- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi every one, Im trying to remove the Z value of all objects within a drawing (similar to flatten).
this is my code, how ever I having a problem and two questions.
namespace Z_remove
{
public class Class1
{
private static HashSet<string> LockedLayers(Database db, Editor ed)
{
var lockedLayer = new HashSet<string>();
using (var tr = db.TransactionManager.StartTransaction())
{
var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
foreach (ObjectId id in layerTable)
{
LayerTableRecord layer = tr.GetObject(id, OpenMode.ForRead) as LayerTableRecord;
if (layer.IsLocked)
{
tr.GetObject(id, OpenMode.ForWrite);
lockedLayer.Add(layer.Name);
}
}
tr.Commit();
return lockedLayer;
}
}
private static void LockUnlockLayers(Database db, Editor ed, HashSet<string> lockedLayers, bool lockLayers)
{
using (var tr = db.TransactionManager.StartTransaction())
{
var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
foreach (ObjectId id in layerTable)
{
LayerTableRecord layer = tr.GetObject(id, OpenMode.ForRead) as LayerTableRecord;
if (lockedLayers.Contains(layer.Name))
{
tr.GetObject(id, OpenMode.ForWrite);
layer.IsLocked = lockLayers;
}
}
tr.Commit();
}
}
[CommandMethod("NSVZ")]
public void RemoveZ()
{
var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
var db = doc.Database;
HashSet<string> lockedLayers = LockedLayers(db, ed);
var lockLayers = false;
LockUnlockLayers(db, ed, lockedLayers, lockLayers);
var sSSet = ed.SelectAll();
using (var tr = db.TransactionManager.StartTransaction())
{
foreach(ObjectId objid in sSSet.Value.GetObjectIds())
{
var ent = (Entity)tr.GetObject(objid, OpenMode.ForWrite);
try
{
ed.Command("CHANGE", objid, "", "P", "E", 0, "","");
ed.WriteMessage("\n--------------------\n");
}
catch (System.Exception ex)
{
ed.WriteMessage("\nexception " + ex.Message);
}
}
tr.Commit();
}
lockLayers = true;
LockUnlockLayers(db, ed, lockedLayers, lockLayers);
}
}
the problem is when running the command every thing works perfectly fine, except there is one block reference which I can see its insertion point has a z=0, but the block reference still has an elevation. the name is *E42 which Is an anonymous block I guess. anonymous block are bad beasts which I cant even find them in the AutoCAD "insert" menu. how can I deal with them?
my first question is, if I have a nested block reference in the drawing, do I have to create a function and set each element's elevation to 0 by using a foreach loop and:
ed.Command("CHANGE", objid, "", "P", "E", 0, "","");
my second question is: is there any better way to set all z values to 0?
thanks in advance
Solved! Go to Solution.