C# - Hatching a block reference.

C# - Hatching a block reference.

jlobo2
Advocate Advocate
1,741 Views
7 Replies
Message 1 of 8

C# - Hatching a block reference.

jlobo2
Advocate
Advocate
 

Hello,

I want to hatch a block reference shown below. I am able to hatch it in solid, but the hatching is displaced by x distance and y height. I feel it's something related to z axis but I am not able to figure it out.

Can you please suggest something. 
Special shout out to @Alexander.Rivilis 

Screenshot_20210113-175105.jpg










Code is below: 

 

 

public void ColorBlock(ObjectId thePANBLKBlockTableRecord, short colorIndex)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // Check to make sure a valid SelectedObject object was returned
                if (thePANBLKBlockTableRecord != null)
                {
                    try
                    {
                        Entity ent = (Entity)tr.GetObject(thePANBLKBlockTableRecord, OpenMode.ForWrite);

                        // Should always be a block reference, but let's make sure
                        BlockReference br = ent as BlockReference;
                        if (br != null)
                        {
                            // Change the color of the block itself
                            br.UpgradeOpen();
                            br.ColorIndex = colorIndex;

                            // Change every entity to be of color ByBlock
                            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(
                                br.BlockTableRecord, OpenMode.ForWrite);

 #region Square                          

                            Point2d pt = new Point2d(br.Position.X, br.Position.Y);
                            Autodesk.AutoCAD.DatabaseServices.Polyline plBox =
                                new Autodesk.AutoCAD.DatabaseServices.Polyline(4);

                            plBox.Normal = Vector3d.ZAxis;
                            plBox.AddVertexAt(0, pt, 0.0, 0, 0);
                            plBox.AddVertexAt(1, new Point2d(pt.X + 4, pt.Y), 0.0, 0, 0);
                            plBox.AddVertexAt(2, new Point2d(pt.X + 4, pt.Y + 4), 0.0, 0, 0);
                            plBox.AddVertexAt(3, new Point2d(pt.X, pt.Y + 4), 0.0, 0, 0);
                            plBox.Closed = true;

                            ObjectId pLineId;
                            pLineId = btr.AppendEntity(plBox);
                            tr.AddNewlyCreatedDBObject(plBox, true);

                            ObjectIdCollection acObjIdColl = new ObjectIdCollection();
                            acObjIdColl.Add(pLineId);

                            Hatch oHatch = new Hatch();
                            Vector3d normal = new Vector3d(0,0, 1);
                            oHatch.Normal = normal;
                            oHatch.Elevation = 0.0;
                            oHatch.PatternScale = 2;
                            oHatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
                            oHatch.ColorIndex = colorIndex;

                            btr.AppendEntity(oHatch);
                            tr.AddNewlyCreatedDBObject(oHatch, true);

                            oHatch.Associative = true;
                            oHatch.AppendLoop((int)HatchLoopTypes.Default, acObjIdColl);
                            oHatch.EvaluateHatch(true);
                            #endregion

#endregion


                            #region Color the boundary red
                            // Iterate through the BlockTableRecord contents
                            foreach (ObjectId id in btr)
                            {
                                // Open the entity
                                Entity ent2 = (Entity)tr.GetObject(id, OpenMode.ForWrite);

                                // Change each entity's color to ByBlock
                                ent2.Color = Color.FromColorIndex(ColorMethod.ByBlock, colorIndex);
                            }
                            #endregion
                        }
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception e)
                    {
                        ed.WriteMessage(e.ToString());
                    }
                }
                tr.Commit();
            }

        }
0 Likes
1,742 Views
7 Replies
Replies (7)
Message 2 of 8

Alexander.Rivilis
Mentor
Mentor

You have to take into account the conversion of the WCS in OCS. br.BlockTransform.Invert return such matrix.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 3 of 8

jlobo2
Advocate
Advocate

Ok, Thank You @Alexander.Rivilis 

Once I do the block transform, how do I access those co-ordinates? 

0 Likes
Message 4 of 8

Alexander.Rivilis
Mentor
Mentor

@jlobo2 wrote:

Ok, Thank You @Alexander.Rivilis 

Once I do the block transform, how do I access those co-ordinates? 


You have not transform block, but you have to transform coordinates with this transformation matrix.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 5 of 8

jlobo2
Advocate
Advocate

Ok. @Alexander.Rivilis ,

 

If you don't mind can you help me out with some code.

Currently I am trying to to this: 

 

 Matrix3d tempMatrix = br.BlockTransform.Inverse();
 Point2d pt =
    new Point2d(
         tempMatrix.CoordinateSystem3d.Origin.X,
         tempMatrix.CoordinateSystem3d.Origin.Y);

         plBox.Normal = new Vector3d(0, 0, 1); //Vector3d.ZAxis;
         plBox.AddVertexAt(0, new Point2d(pt.X , pt.Y  ), 0.0, 0, 0);
         plBox.AddVertexAt(1, new Point2d(pt.X + 4, pt.Y ), 0.0, 0, 0);
         plBox.AddVertexAt(2, new Point2d(pt.X + 4, pt.Y + 4), 0.0, 0, 0);
         plBox.AddVertexAt(3, new Point2d(pt.X, pt.Y + 4), 0.0, 0, 0);
         plBox.Closed = true;

 

0 Likes
Message 6 of 8

Alexander.Rivilis
Mentor
Mentor

Totally wrong.  You have to use pt.TransformBy() method (but instead of Point2d you have to use Point3d)

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 7 of 8

jlobo2
Advocate
Advocate

Ok, @Alexander.Rivilis 

Something like this?

 

Matrix3d tempMatrix = br.BlockTransform.Inverse();
PlanarEntity tempPlanarEntity = br.GetPlane();

Point2d pt2D =  
     new Point2d(
         tempMatrix.CoordinateSystem3d.Origin.X, 
          tempMatrix.CoordinateSystem3d.Origin.Y);

Point3d pt3D = new Point3d(tempPlanarEntity, pt2D);
Autodesk.AutoCAD.DatabaseServices.Polyline plBox =
    new Autodesk.AutoCAD.DatabaseServices.Polyline(4);
     plBox.Normal = new Vector3d(0, 0, 1); //Vector3d.ZAxis;
     plBox.AddVertexAt(0, new Point2d(pt3D.X , pt3D.Y  ), 0.0, 0, 0);
     plBox.AddVertexAt(1, new Point2d(pt3D.X + 4, pt3D.Y ), 0.0, 0, 0);
     plBox.AddVertexAt(2, new Point2d(pt3D.X + 4, pt3D.Y + 4), 0.0, 0, 0);
     plBox.AddVertexAt(3, new Point2d(pt3D.X, pt3D.Y + 4), 0.0, 0, 0);
     plBox.Closed = true;

 

0 Likes
Message 8 of 8

jlobo2
Advocate
Advocate

Great, @Alexander.Rivilis 

I got that to work. Thank You for your help.

Getting the plane worked.

jlobo2_1-1610634476799.png

 

 

 

0 Likes