.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Erase Hatch using Polyline ObjectId

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
nagarjuna009k
2311 Views, 10 Replies

Erase Hatch using Polyline ObjectId

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

 

 

 

- Nag
Tags (3)
10 REPLIES 10
Message 2 of 11
Hallex
in reply to: nagarjuna009k

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'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 11
nagarjuna009k
in reply to: Hallex

Hello Hallex,

 

Thanks for your information.

 

But here I have few problems. 

 

  • Hatch erasing process using Polyline ObjectId should be done automatically, no user interaction is required.
  • As I have Polyline ObjectId, I tried to collect the points from the Polyline used it in fence selection. This fence selection will work only when there is no adjusent Hatch attached to this Polyline. For your understanding I have attached a sample drawing which shows a set of continious polylines with hatches. Now I want to erase first Hatch in drawing (Pink color) and I have corresponding Polyline objectid. If I use fence selection in this requirement, adjuscent hatch is also getting selected. 

So Is there any other way to erase the Hatch using Polyline ObjectId?

 

Thanks for your help.

Nag

- Nag
Message 4 of 11
Hallex
in reply to: nagarjuna009k

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'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 5 of 11
_gile
in reply to: nagarjuna009k

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();
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 11
Hallex
in reply to: _gile

gile,

it's working at all for this drawing, check yourself, please

 

~'J'~

 

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 7 of 11
_gile
in reply to: Hallex

As I said, this will only work with associative hatches.
In the attached drawing, only the left most one in associative (there're 2 duplicated).

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 11
Hallex
in reply to: _gile

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

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 9 of 11
nagarjuna009k
in reply to: Hallex

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;

 

- Nag
Message 10 of 11
Hallex
in reply to: nagarjuna009k

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'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 11 of 11
nagarjuna009k
in reply to: Hallex

Hallex,

 

Thanks for your time and your code. I'll try tomorrow and let you know if this works better.

 

Thanks

Nag`

- Nag

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost