Author Topic: Associative Hatch  (Read 1496 times)

0 Members and 1 Guest are viewing this topic.

ahlzl

  • Guest
Associative Hatch
« on: July 18, 2008, 08:21:07 PM »
I succeeded because I had Daniel and many friends to help me.
thanks again !

put Hatch.arx to AutoCAD 2009 installation path

Code: [Select]
using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace AssociativeHatch
{
    public class Class1
    {
        [DllImport("Hatch.arx", CallingConvention = CallingConvention.Cdecl,
            EntryPoint = "AddPersistentReactor")]

        private static extern void AddPersistentReactor(IntPtr intptr, ObjectId objId);
        private static void AddPersistent(Entity ent, ObjectId objId)
        {
            AddPersistentReactor(ent.UnmanagedObject, objId);
        }

        [CommandMethod("test")]
        public void Test()
        {
            Database db = HostApplicationServices.WorkingDatabase;

            Line line1 = new Line(new Point3d(10, 0, 0), new Point3d(0, 0, 0));
            Line line2 = new Line(new Point3d(10, 0, 0), new Point3d(8, 6, 0));
            Line line3 = new Line(new Point3d(8, 6, 0), new Point3d(0, 0, 0));
            Circle circle1 = new Circle(new Point3d(15, 5, 0), Vector3d.ZAxis, 4);

            ObjectId loopId1 = AppendEntity(line1);
            ObjectId loopId2 = AppendEntity(line2);
            ObjectId loopId3 = AppendEntity(line3);
            ObjectId loopId4 = AppendEntity(circle1);

            ObjectIdCollection loops1 = new ObjectIdCollection();
            loops1.Add(loopId1);
            loops1.Add(loopId2);
            loops1.Add(loopId3);
            ObjectIdCollection loops2 = new ObjectIdCollection();
            loops2.Add(loopId4);

            ObjectIdCollection[] loops = new ObjectIdCollection[2];
            loops.SetValue(loops1, 0);
            loops.SetValue(loops2, 1);

            ObjectId hatchId = AddHatch(loops, 0, "ANGLE", Math.PI / 6, 0.2);


            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                Entity tempEnt;
                ObjectIdCollection tempArr;
                for (int n = 0; n < loops.Length; n++)
                {
                    tempArr = (ObjectIdCollection)loops.GetValue(n);
                    for (int m = 0; m < tempArr.Count; m++)
                    {
                        tempEnt = (Entity)trans.GetObject(tempArr[m], OpenMode.ForWrite);
                        AddPersistent(tempEnt, hatchId);
                    }
                }
                trans.Commit();
            }
        }

        static private ObjectId AddHatch(ObjectIdCollection[] objIds, HatchPatternType patType,
            string patName, double patternAngle, double patternScale)
        {
            try
            {
                Hatch ent = new Hatch();
                ent.HatchObjectType = HatchObjectType.HatchObject;
                Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)trans.GetObject
                        (db.BlockTableId, OpenMode.ForRead);
                    BlockTableRecord btr = (BlockTableRecord)trans.GetObject
                        (bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    ObjectId entId = btr.AppendEntity(ent);
                    trans.AddNewlyCreatedDBObject(ent, true);
                    ent.PatternAngle = patternAngle;
                    ent.PatternScale = patternScale;
                    ent.SetHatchPattern(patType, patName);
                    ent.Associative = true;
                    for (int i = 0; i < objIds.Length; i++)
                    {
                        ent.InsertLoopAt(i, HatchLoopTypes.Default, objIds[i]);
                    }
                    trans.Commit();
                    return entId;
                }
            }
            catch
            {
                ObjectId nullId = ObjectId.Null;
                return nullId;
            }
        }

        static ObjectId AppendEntity(Entity ent)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            ObjectId entId;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,
                    OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject
                    (bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                entId = btr.AppendEntity(ent);
                trans.AddNewlyCreatedDBObject(ent, true);
                trans.Commit();
            }
            return entId;
        }
    }
}


ahlzl

  • Guest
Re: Associative Hatch
« Reply #1 on: July 18, 2008, 08:26:24 PM »
look:

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6963
  • AKA Daniel
Re: Associative Hatch
« Reply #2 on: July 19, 2008, 08:15:24 AM »
Very nice work indeed!  8-)
 Thank you for sharing  :-)

fixo

  • Guest
Re: Associative Hatch
« Reply #3 on: July 19, 2008, 08:41:39 AM »
Sadly for me, I use A2008 so
it's not working :oops:

~'J'~

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16723
  • Superior Stupidity at its best
Re: Associative Hatch
« Reply #4 on: July 19, 2008, 08:48:17 AM »
Is this different from the built in associative hatch?
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal