Author Topic: Create associative hatch  (Read 2041 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Create associative hatch
« on: January 04, 2013, 07:44:39 PM »
Code: [Select]
        [CommandMethod("CreateAssocHatch","cash", CommandFlags.UsePickSet)]
        public void testCreateAssocHatch()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            ObjectId objId;

            Entity ent;

            PromptEntityResult per = ed.GetEntity(new PromptEntityOptions("\nSelect an entity to be hatched: "));

            if (per.Status != PromptStatus.OK)  return;

            objId = per.ObjectId;

            try
            {
                Transaction tr = doc.TransactionManager.StartTransaction();

                using (tr)
                {

                    ent = tr.GetObject(objId, OpenMode.ForRead) as Entity;

                    if (ent == null) return;
                 
                    Hatch hat = new Hatch();

                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
                   // to set associative add hatch first
                    btr.AppendEntity(hat);

                    tr.AddNewlyCreatedDBObject(hat, true);

                    hat.PatternAngle = 0.0;

                    hat.PatternScale = 10.0;

                    hat.Associative = true;

                    hat.SetHatchPattern(HatchPatternType.PreDefined, "ANSI32");

                    // append loop to hatch
                    ObjectIdCollection dbObjIds = new ObjectIdCollection();

                    dbObjIds.Add(objId);
                     
                    hat.AppendLoop(HatchLoopTypes.External, dbObjIds);
       
                    hat.Layer = ent.Layer;
 
                    hat.ColorIndex = 256;//bylayer,  may use as per entity colorindex
   
                    hat.HatchStyle = HatchStyle.Normal;
 
                    hat.EvaluateHatch(true);
 

                    tr.Commit();
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                ed.WriteMessage("Error: {0}", ex.ToString());
            }
        }

Offline bcinnv

  • Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
    • prefered language: VB
    • Prog expertise: Good
    • View Profile
Re: Create associative hatch
« Reply #1 on: March 26, 2013, 01:21:28 PM »
Thanks fixo!  I'm tired of using the built in hatch functions... its automatic selection almost always crashes my session.