Insert dynamic block with hatch

Insert dynamic block with hatch

KZambaux
Enthusiast Enthusiast
1,097 Views
4 Replies
Message 1 of 5

Insert dynamic block with hatch

KZambaux
Enthusiast
Enthusiast

Hi,

 

I am stuck with a dynamic block with hatch.

If I insert it manually and I change the length parameter named 'Distance1', I got the proper behaviour. Hatch follow the boundary.

However, if I insert it by code and I change the length parameter, hatch doesn't follow boundaries.

Here is the result:

 

BlockPN_AfterInsertion.PNG

 

I have attached the block.

 

I did follow the hint (remove the hatch from the stretch action selection) described in

https://forums.autodesk.com/t5/net/update-dynamic-block-hatch/m-p/8681308

but then I have another weird behaviour. Hatch doesn't follow boundary neither the block itself when I change manually the length.

 

Between the code and the way to create the block, I am lost.

 

Here is the code to insert block:

'fichier' is the path of the block

'infobloc' is some comment to add

'point' is the position of the block

'onglet' is the name of the layout to insert block (BlockTableRecord.ModelSpace)

'calque' is the name of the layer where to add the block

 public static ObjectId InsertionSideDataBase(string fichier, string infobloc, Point2d point, string onglet, Database database, string calque)
        {
            if (File.Exists(fichier))
            {
                ObjectId blkid;
                ObjectId returnID = ObjectId.Null;

                using (Database bdb = new Database(false, true))
                {
                    using (Transaction tr = database.TransactionManager.StartTransaction())
                    {
                        bdb.ReadDwgFile(fichier, System.IO.FileShare.Read, true, "");
                        if (infobloc == null)
                            blkid = database.Insert(string.Format("{0}", System.IO.Path.GetFileNameWithoutExtension(fichier)), bdb, true);
                        else
                            blkid = database.Insert(string.Format("{0} {1}", System.IO.Path.GetFileNameWithoutExtension(fichier), infobloc), bdb, true);

                        BlockTableRecord acBlkTblRec;
                        acBlkTblRec = tr.GetObject(blkid, OpenMode.ForRead) as BlockTableRecord;

                        BlockReference acBlkRef = new BlockReference(TransformPoint2d_3d(point, 0), blkid);

                        // Open the Block table record for read
                        BlockTable acBlkTbl;
                        acBlkTbl = tr.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;

                        // Open the Block table record Model space for write
                        BlockTableRecord acBlkTblRec1;
                        acBlkTblRec1 = tr.GetObject(acBlkTbl[onglet], OpenMode.ForWrite) as BlockTableRecord;

                        using (acBlkRef)
                        {
                            //BlockTableRecord accurspaceblktblrecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                            returnID = acBlkTblRec1.AppendEntity(acBlkRef);
                            tr.AddNewlyCreatedDBObject(acBlkRef, true);

                            acBlkRef.Layer = calque;

                            if (acBlkTblRec.HasAttributeDefinitions)
                            {
                                // Add attributes from the block table record
                                foreach (ObjectId objID in acBlkTblRec)
                                {
                                    DBObject dbObj = tr.GetObject(objID, OpenMode.ForRead) as DBObject;

                                    if (dbObj is AttributeDefinition)
                                    {
                                        AttributeDefinition acAtt = dbObj as AttributeDefinition;

                                        if (!acAtt.Constant)
                                        {
                                            using (AttributeReference acAttRef = new AttributeReference())
                                            {
                                                acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
                                                acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
                                                acBlkRef.AttributeCollection.AppendAttribute(acAttRef);
                                                tr.AddNewlyCreatedDBObject(acAttRef, true);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        tr.Commit();
                    }
                }
                return returnID;
            }
            else
            {
                string message = String.Format("Le fichier {0} n'existe pas", fichier);
                throw new ArgumentException(message);
            }
        }

 

here is the code to modify the length parametre:

'iD_Dessin' is the ObjectId of the bloc I want to change

'parameter' is the name of the parameter to update (Distance1 in my case)

'valeur' is the new value

 public static void EcrireParametreSideDataBase(ObjectId iD_Dessin, string parametre, object valeur)
        {
            using (Transaction acTrans = iD_Dessin.Database.TransactionManager.StartTransaction())
            {
                Entity acEnt = acTrans.GetObject(iD_Dessin, OpenMode.ForWrite) as Entity;

                if (acEnt != null)
                {
                    BlockReference br = acEnt as BlockReference;
                    if (br != null)
                    {
                        foreach (DynamicBlockReferenceProperty dbrp in br.DynamicBlockReferencePropertyCollection)
                        {
                            if (dbrp.PropertyName == parametre)
                            {
                                dbrp.Value = valeur;
                            }
                        }
                    }
                }
                acTrans.Commit();
            }
        }

 

 

Here there something special to manage hatch in dynamic block?

 

Thanks in advance for your help

 

Kevin

0 Likes
1,098 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor

Well, when manually dragging the grip, the hatch changes correctly, but if you change the dynamic property in Properties window, the hatch would behave as you described. However, when setting the property with code, I could not reproduce the behavior. I tried with your block and a block I created that is similar to yours. Here is the code I used for test:

 

 

        [CommandMethod("SetBlkHatch")]
        public static void SetDynHatchInBlock()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
            var res = ed.GetEntity("\nSelect hatched block:");
            if (res.Status== PromptStatus.OK)
            {
                while(true)
                {
                    if (!SetDynProp(ed, res.ObjectId))
                    {
                        break;
                    }
                    var opt = new PromptKeywordOptions("\nDo it again?");
                    opt.Keywords.Add("Yes");
                    opt.Keywords.Add("No");
                    opt.Keywords.Default = "Yes";
                    var kres = ed.GetKeywords(opt);
                    if (kres.Status== PromptStatus.OK)
                    {

                    }
                    else
                    {
                        break;
                    }
                }
            }
        }

        private static bool SetDynProp(Editor ed, ObjectId blkId)
        {
            var opt = new PromptDoubleOptions("\nEnter value (1.0 to 10.0):");
            opt.AllowZero = false;
            opt.AllowNegative = false;
            opt.AllowArbitraryInput = false;

            var res = ed.GetDouble(opt);
            if (res.Status== PromptStatus.OK)
            {
                using (var tran = blkId.Database.TransactionManager.StartTransaction())
                {
                    var blk = (BlockReference)tran.GetObject(blkId, OpenMode.ForWrite);

                    foreach (DynamicBlockReferenceProperty prop in
                            blk.DynamicBlockReferencePropertyCollection)
                    {
                        if (prop.PropertyName.ToUpper() == "DISTANCE1")
                        {
                            prop.Value = res.Value;
                            break;
                        }
                    }

                    tran.Commit();
                }

                return true;
            }
            else
            {
                return false;
            }
        }

 

 

Here is the video clip showing the test runs:

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

KZambaux
Enthusiast
Enthusiast

Hi,

Thanks for your time and anwser.

I run some tests:

  • I insert my block manually, then I use your code. The behaviour is OK.
  • I insert my block by code in side-database. Then I open the dwg file. And then I use your code. Behaviour is OK
  • But when I insert my block by code in side-database, then I change the length, the behaviour is still not OK

I guess I must forget something when I insert block. But I don't know what.

 

Anyway, instead of using a hatch, we may go for a polyline with an appropriate thickness. It is easier to deal with polyline than hatch.

 

If someone find a solution about it, I will glad to hear it.

0 Likes
Message 4 of 5

light_dark_24
Community Visitor
Community Visitor

Hi maybe it's been a while now, but i had  the same problem as you, and check your block. In the end, the problem occurs if the hatch you use is associative, it cannot be part of the stretch selection.

0 Likes
Message 5 of 5

KZambaux
Enthusiast
Enthusiast

hi,

I read that the hatch must be associative....

Anyway, can you post your block and code? I may give a try with it.

 

0 Likes