Create Block with Hatch

Create Block with Hatch

vkpunique
Advocate Advocate
1,771 Views
5 Replies
Message 1 of 6

Create Block with Hatch

vkpunique
Advocate
Advocate

Hi, I am Trying to create Block with hatch inside them. I can Create Block with Polyline or any other entity but no Sucess with Hatch. Can Anyone Provide Right Reference for this problem?

Why No documentation available for this? It Very common use case for any drawing.

0 Likes
Accepted solutions (1)
1,772 Views
5 Replies
Replies (5)
Message 3 of 6

vkpunique
Advocate
Advocate

Hi, @_gile . I mean Insert new Block Defination with Hatch. Sorry For Bad title

0 Likes
Message 4 of 6

_gile
Consultant
Consultant

What's your problem? Creating a block definition which contains a Hatch ou inserting such block ?

Please elaborate and/or provide some code.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 6

vkpunique
Advocate
Advocate

Hi @_gile ,

I am Trying to Create Block Definition with Hatch. I am working on it for last few hours. Finally I've found below code which is working but I have to create whole drawing first before creating block from it. Is there any better way to do this?

Check out Sample Code in Attached File.  I am looking for Any Improvement or Alternative to that code ?

 

0 Likes
Message 6 of 6

_gile
Consultant
Consultant
Accepted solution

You add a Hatch in a block definition (BlockTableRecord) exactly the same way as you do it in the model space (BlockTableRecord)

From this reply:

 

private static ObjectId CreateBlockWithHatch(Database db, string blockName)
        {
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord block;
                if (bt.Has(blockName)) return bt[blockName];

                tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
                block = new BlockTableRecord();
                block.Name = blockName;
                var blockId = bt.Add(block);
                tr.AddNewlyCreatedDBObject(block, true);

                var pline = new Polyline(4) { Closed = true, Layer = "0" };
                pline.Layer = "0";
                pline.AddVertexAt(0, new Point2d(-12.0, -12.0), 0.0, 0.0, 0.0);
                pline.AddVertexAt(0, new Point2d(12.0, -12.0), 0.0, 0.0, 0.0);
                pline.AddVertexAt(0, new Point2d(12.0, 12.0), 0.0, 0.0, 0.0);
                pline.AddVertexAt(0, new Point2d(-12.0, 12.0), 0.0, 0.0, 0.0);
                block.AppendEntity(pline);
                tr.AddNewlyCreatedDBObject(pline, true);

                var circle = new Circle() { Center = Point3d.Origin, Radius = 8.0, Layer = "0" };
                block.AppendEntity(circle);
                tr.AddNewlyCreatedDBObject(circle, true);
                var ids = new ObjectIdCollection(new[] { circle.ObjectId });

                var hatch = new Hatch() { Layer = "0", PatternScale = 0.5, ColorIndex = 1 };
                hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
                block.AppendEntity(hatch);
                tr.AddNewlyCreatedDBObject(hatch, true);
                hatch.Associative = true;
                hatch.AppendLoop(HatchLoopTypes.Default, ids);
                hatch.EvaluateHatch(true);

                tr.Commit();
                return blockId;
            }
        }

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes