Hello All,
I have a drawing with a Polyline filled with Hatch. Also I have that Polyline ObjectId. So using this ployline ObjectId I want to erase the Hatch inside polyline. Can anyone give an idea how I can erase the Hatch.
I'm developing the application using C#.
Thanks,
Nag
Solved! Go to Solution.
I have idea to select hatch by fence selection
here is a quick demo example
[CommandMethod("ehatch")] public void eraseHatch() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; Point3d p1; Point3d p2; try { using (Transaction tr = db.TransactionManager.StartTransaction()) { //------------------------------------------------------------------ // Create filter object, this filter can have any types to select, i.e. lines SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "lwpolyline"), new TypedValue(70, 1) }); // Select lines on screen PromptSelectionResult psr = ed.GetSelection(filter); // Check if selection is succeed if (psr.Status != PromptStatus.OK) return; // if OK then iterate trough selected lines foreach (SelectedObject selobj in psr.Value) { // Cast SelectedObject as DBObject (or as an Entity) DBObject obj = tr.GetObject(selobj.ObjectId, OpenMode.ForRead) as DBObject; if (obj != null) { // Cast DBObject as Polyline Polyline pline = obj as Polyline; p1 = pline.GetPoint3dAt(0); p2 = pline.GetPoint3dAt(1); Point3dCollection pts = new Point3dCollection(); pts.Add(p1); pts.Add(p2); // select hatch by fence filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "hatch") }); psr = ed.SelectFence(pts,filter); SelectionSet hset = psr.Value; ObjectId[] ids= hset.GetObjectIds(); ObjectId hid = ids[0]; DBObject hobj = tr.GetObject(hid, OpenMode.ForRead) as DBObject; Hatch inhatch = hobj as Hatch; hobj.UpgradeOpen(); hobj.Erase(); } } tr.Commit(); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage (ex.Message + "\n" + ex.StackTrace); } finally { } }
~'J'~
Hello Hallex,
Thanks for your information.
But here I have few problems.
So Is there any other way to erase the Hatch using Polyline ObjectId?
Thanks for your help.
Nag
Well, I see this hatch is adjanced and associative,
one possiible way is to perform command twice, I'm pretty
sure there is other way to do the job, but I don't know how yet
[CommandMethod("ehatch")] public void eraseHatch() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; Point3d p1; Point3d p2; try { using (Transaction tr = db.TransactionManager.StartTransaction()) { //------------------------------------------------------------------ // Create filter object, this filter can have any types to select, i.e. lines SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "lwpolyline"), new TypedValue(70, 1) }); // Select lines on screen PromptSelectionResult psr = ed.GetSelection(filter); // Check if selection is succeed if (psr.Status != PromptStatus.OK) return; // if OK then iterate trough selected lines foreach (SelectedObject selobj in psr.Value) { // Cast SelectedObject as DBObject (or as an Entity) DBObject obj = tr.GetObject(selobj.ObjectId, OpenMode.ForRead) as DBObject; if (obj != null) { // Cast DBObject as Polyline Polyline pline = obj as Polyline; p1 = pline.GetPoint3dAt(0); p2 = pline.GetPoint3dAt(1); Point3dCollection pts = new Point3dCollection(); pts.Add(p1); pts.Add(p2); // select hatch by fence filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "hatch") }); psr = ed.SelectFence(pts, filter); SelectionSet hset = psr.Value; ObjectId[] ids = hset.GetObjectIds(); ObjectId hid = ids[0]; DBObject hobj = tr.GetObject(hid, OpenMode.ForRead) as DBObject; Hatch inhatch = hobj as Hatch; doc.TransactionManager.EnableGraphicsFlush(true); inhatch.UpgradeOpen(); if (inhatch.Associative) { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Perform command \"ehatch\" twice"); pline.UpgradeOpen(); inhatch.Associative = false; inhatch.RemoveAssociatedObjectIds(); inhatch.EvaluateHatch(true); inhatch.RecordGraphicsModified(true); tr.TransactionManager.QueueForGraphicsFlush(); } ed.Regen(); inhatch.Erase(); } } doc.TransactionManager.FlushGraphics(); tr.Commit(); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage (ex.Message + "\n" + ex.StackTrace); } finally { } }
~'J'~
Hi,
If the Hatch is associative, you can use the PersistentReactorId.
Here's a little sample:
private void EraseAssocHatch(ObjectId sourceId) { Database db = sourceId.Database; using (Transaction tr = db.TransactionManager.StartTransaction()) { Entity ent = (Entity)tr.GetObject(sourceId, OpenMode.ForRead); foreach (ObjectId id in ent.GetPersistentReactorIds()) { if (id.ObjectClass.Name == "AcDbHatch") { Hatch h = (Hatch)tr.GetObject(id, OpenMode.ForWrite); h.Erase(); } } tr.Commit(); } }
gile,
it's working at all for this drawing, check yourself, please
~'J'~
Hi _gile,
Thanks for explanation, maybe this happens after I've converted the drawing
with TreeView add-in on my A2009 format, so the code is not working on my machine
Regards,
Oleg
Dear All,
I found a solution for my problem using SelectCrossingWindow.
Here is the code which I used.
Thanks for you help.
Nag
Polyline pl = (Polyline)ent; Point3d plCentroid = oStdFunc.GetCentroid(pl); Point3d p1 = new Point3d(plCentroid.X - 50, plCentroid.Y - 50, 0.0); Point3d p2 = new Point3d(plCentroid.X + 50, plCentroid.Y + 50, 0.0); TypedValue[] values = { new TypedValue((int)DxfCode.Start, "HATCH") }; SelectionFilter filter = new SelectionFilter(values); PromptSelectionResult psr = ed.SelectCrossingWindow(p1, p2, filter); SelectionSet sSet = psr.Value; oldHatchId = sSet[0].ObjectId;
Gla you solved it by yourself
See if this code would be work better:
[CommandMethod("ehatch")] public void eraseHatch() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; Point3d p1; Point3d p2; Point3d p3; Point3d p4; try { using (Transaction tr = db.TransactionManager.StartTransaction()) { //------------------------------------------------------------------ // Create filter object, this filter can have any types to select, i.e. lwpolyline SelectionFilter filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "lwpolyline"), new TypedValue(70, 1) }); // Select lines on screen PromptSelectionResult psr = ed.GetSelection(filter); // Check if selection is succeed if (psr.Status != PromptStatus.OK) return; // if OK then iterate trough selected polylines foreach (SelectedObject selobj in psr.Value) { // Cast SelectedObject as DBObject (or as an Entity) DBObject obj = tr.GetObject(selobj.ObjectId, OpenMode.ForRead) as DBObject; if (obj != null) { // Cast DBObject as Polyline Polyline pline = obj as Polyline; p1 = pline.GetPoint3dAt(0); p2 = pline.GetPoint3dAt(1); p3 = p1 + (p2 - p1).DivideBy(8); p4 = p2 + (p1 - p2).DivideBy(8); Point3dCollection pts = new Point3dCollection(); pts.Add(p3); pts.Add(p4); // select hatch by fence filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "hatch") }); psr = ed.SelectFence(pts, filter); if (psr.Status != PromptStatus.OK) return; SelectionSet hset = psr.Value; // if OK then iterate trough selected hatches foreach (SelectedObject sobj in hset) { // Cast SelectedObject as DBObject (or as an Entity) DBObject hobj = tr.GetObject(sobj.ObjectId, OpenMode.ForRead) as DBObject; if (hobj != null) { Hatch inhatch = hobj as Hatch; inhatch.UpgradeOpen(); inhatch.Associative = false; inhatch.Erase(); tr.TransactionManager.QueueForGraphicsFlush(); } } } doc.TransactionManager.FlushGraphics(); tr.Commit(); } } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage (ex.Message + "\n" + ex.StackTrace); } finally { } }
~'J'~
Hallex,
Thanks for your time and your code. I'll try tomorrow and let you know if this works better.
Thanks
Nag`
Can't find what you're looking for? Ask the community or share your knowledge.