.NET

Reply
Member
nagarjuna009k
Posts: 4
Registered: ‎09-30-2008
Message 1 of 11 (592 Views)
Accepted Solution

Erase Hatch using Polyline ObjectId

592 Views, 10 Replies
08-18-2012 09:00 AM

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

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;

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 11 (576 Views)

Re: Erase Hatch using Polyline ObjectId

08-18-2012 02:35 PM 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
Member
nagarjuna009k
Posts: 4
Registered: ‎09-30-2008
Message 3 of 11 (564 Views)

Re: Erase Hatch using Polyline ObjectId

08-18-2012 07:44 PM 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
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 11 (555 Views)

Re: Erase Hatch using Polyline ObjectId

08-18-2012 11:29 PM 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
*Expert Elite*
_gile
Posts: 2,104
Registered: ‎04-29-2006
Message 5 of 11 (549 Views)

Re : Erase Hatch using Polyline ObjectId

08-19-2012 01:14 AM 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
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 6 of 11 (544 Views)

Re : Erase Hatch using Polyline ObjectId

08-19-2012 04:41 AM in reply to: _gile

gile,

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

 

~'J'~

 

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
*Expert Elite*
_gile
Posts: 2,104
Registered: ‎04-29-2006
Message 7 of 11 (540 Views)

Re : Erase Hatch using Polyline ObjectId

08-19-2012 05:05 AM 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
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 8 of 11 (536 Views)

Re : Erase Hatch using Polyline ObjectId

08-19-2012 05:57 AM 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
Member
nagarjuna009k
Posts: 4
Registered: ‎09-30-2008
Message 9 of 11 (526 Views)

Re : Erase Hatch using Polyline ObjectId

08-19-2012 07:05 AM 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
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 10 of 11 (518 Views)

Re : Erase Hatch using Polyline ObjectId

08-19-2012 08:37 AM 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
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.