Unable create Hatch after to Windows Form

Unable create Hatch after to Windows Form

traiduong014969
Collaborator Collaborator
728 Views
7 Replies
Message 1 of 8

Unable create Hatch after to Windows Form

traiduong014969
Collaborator
Collaborator

Hi all,

I Started work with Windonw Form. After create Hatch for a object. I begine make a form to seclect Hatch. However when i test code that has appear a message as picture below:

traiduong014969_0-1676469860893.png

 

 

-moderator edit: corrected spelling errors in title.

So everyone can tell me how to fix that ?

Thank you !

 

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void B2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void B3_Click(object sender, EventArgs e)
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            string mh = B1.SelectedItem.ToString();



            try
            {
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = trans.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                    Polyline pl = new Polyline();
                    pl.SetDatabaseDefaults();
                    pl.AddVertexAt(0, new Point2d(2.0, 4.0), 0, 0, 0);
                    pl.AddVertexAt(1, new Point2d(4.0, 2.0), 0, 0, 0);
                    pl.AddVertexAt(2, new Point2d(6.0, 4.0), 0, 0, 0);
                    pl.Closed = true;
                    pl.ColorIndex = 6;

                    // Add the polyline to the database
                    ObjectId plineId = btr.AppendEntity(pl);
                    trans.AddNewlyCreatedDBObject(pl, true);

                    // Create the hatch object
                    Hatch ht = new Hatch();
                    ht.SetHatchPattern(HatchPatternType.PreDefined, "mh");
                    ht.ColorIndex = 54;
                    ht.BackgroundColor = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 4);

                    // Add the hatch to the database
                    btr.AppendEntity(ht);
                    trans.AddNewlyCreatedDBObject(ht, true);

                    // Set the hatch boundary
                    ht.Associative = true;
                    ht.AppendLoop(HatchLoopTypes.Default, new ObjectIdCollection() { plineId });
                    ht.EvaluateHatch(true);

                    // Add dimention
                    AlignedDimension al = new AlignedDimension();
                    AlignedDimension al1 = new AlignedDimension();
                    AlignedDimension al2 = new AlignedDimension();

                    al.XLine1Point = new Point3d(2, 4, 0);
                    al.XLine2Point = new Point3d(4, 2, 0);
                    al.Dimscale = 0.5;
                    al.DimensionStyle = db.Dimstyle;

                    al1.XLine1Point = new Point3d(4, 2, 0);
                    al1.XLine2Point = new Point3d(6, 4, 0);
                    al1.Dimscale = 0.5;
                    al1.DimensionStyle = db.Dimstyle;

                    al2.XLine1Point = new Point3d(2, 4, 0);
                    al2.XLine2Point = new Point3d(6, 4, 0);
                    al2.Dimscale = 0.5;
                    al2.DimensionStyle = db.Dimstyle;


                    // Add the new object to Model space and the transaction
                    btr.AppendEntity(al);
                    trans.AddNewlyCreatedDBObject(al, true);

                    btr.AppendEntity(al1);
                    trans.AddNewlyCreatedDBObject(al1, true);

                    btr.AppendEntity(al2);
                    trans.AddNewlyCreatedDBObject(al2, true);
                    trans.Commit();
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                AcAp.ShowAlertDialog("not create hatch from poplyline " + ex.Message);
            }
        }

        private void B1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
    }

 

 

0 Likes
Accepted solutions (1)
729 Views
7 Replies
Replies (7)
Message 2 of 8

hippe013
Advisor
Advisor

It would be helpful if you posted your code in its entirety. How are you launching your dialog? Modal? Modeless? Without seeing the rest of your code I have to just guess. At first glance the problem appears to be that you are not locking your document. 

 

Using DocumentLock docLock = doc.LockDocument {
   Using Transaction trans = db.TransactionManager.StartTransaction {

   }
}

 

0 Likes
Message 3 of 8

hosneyalaa
Advisor
Advisor

Hi

I think you are needing

 

a LockDocument() method.

 

And change

 

// Add the polyline to the database
                   btr.AppendEntity(pl);
                    trans.AddNewlyCreatedDBObject(pl, true);

 

 

And  can you see this

 

https://forums.autodesk.com/t5/net/draw-polyline-with-hatch-getting-error-einvalidinput/m-p/9632916

 

0 Likes
Message 4 of 8

traiduong014969
Collaborator
Collaborator

When i replace Text "mh" by "ANSI31" or specific name. Code woking, but i replace by a variable. It will appear a error.

traiduong014969_0-1676472642615.png

 

0 Likes
Message 5 of 8

traiduong014969
Collaborator
Collaborator

It's seem that has an error when i add DoccumentLock

traiduong014969_0-1676476293734.png

 

0 Likes
Message 6 of 8

Ed__Jobe
Mentor
Mentor
"not create hatch from poplyline " should be "Hatch not created from polyline". Polyline only has 1 "p".

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 7 of 8

hosneyalaa
Advisor
Advisor

What It is in the error message

add DoccumentLock 

As @hippe013  write 

0 Likes
Message 8 of 8

traiduong014969
Collaborator
Collaborator
Accepted solution

This mistake is add apostrophes into "mh"

ht.SetHatchPattern(HatchPatternType.PreDefined, "mh");

code after fixed

ht.SetHatchPattern(HatchPatternType.PreDefined, mh);

 

0 Likes