Author Topic: BLOCKHEADS: Managing Dynamic Block Refs' Visibility State  (Read 10700 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: BLOCKHEADS: Managing Dynamic Block Refs' Visibility State
« Reply #15 on: May 04, 2012, 04:48:56 PM »

< .. >

For just finding out if a block is referenced or not, Database.Purge()
should do that, but I've not tested it with dynamic blocks.

That's some interesting cat skinning :)

Regards
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

serga

  • Guest
Re: BLOCKHEADS: Managing Dynamic Block Refs' Visibility State
« Reply #16 on: May 07, 2012, 12:55:32 AM »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: BLOCKHEADS: Managing Dynamic Block Refs' Visibility State
« Reply #17 on: May 07, 2012, 01:17:30 AM »

The link works for me.

The contained document seems complete to me.

In what way is it not viable for you ?

Regards
Kerry
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

serga

  • Guest
Re: BLOCKHEADS: Managing Dynamic Block Refs' Visibility State
« Reply #18 on: May 07, 2012, 01:51:32 AM »
Sorry, You're right, the problem is in the proxy of the company, I downloaded from home without any problems.
Thanks.

serga

  • Guest
Re: BLOCKHEADS: Managing Dynamic Block Refs' Visibility State
« Reply #19 on: May 07, 2012, 04:58:41 AM »
If I unerstand right this code may helps, try it
Code: [Select]

        public static void UpdateSwitch(Database db, BlockReference bref, string pname, string pvalue)
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {

                    TypedValue[] filterlist = new TypedValue[2];

                    filterlist[0] = new TypedValue(0, "INSERT");

                    filterlist[1] = new TypedValue(2, "`*U*,switch");

                    SelectionFilter filter = new SelectionFilter(filterlist);

                    PromptSelectionResult res = ed.SelectAll(filter);

                    if (res.Status != PromptStatus.OK)
                    {

                        ed.WriteMessage("\nNo \"switch\" block references selected");

                        return;

                    }

                    if (res.Value.Count != 0)
                    {
                        SelectionSet select = res.Value;

                        ed.WriteMessage("\nSelected:\t{0}", select.GetObjectIds().Count());

                        foreach (ObjectId brId in select.GetObjectIds())
                        {
                            BlockReference blkRef = (BlockReference)tr.GetObject(brId, OpenMode.ForRead);
                            // Get the dynamic block's property collection
                            if (blkRef.IsDynamicBlock)
                            {
                                BlockTableRecord btr = tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead, false) as BlockTableRecord;
                                if (btr.Name == "switch")
                                {
                                    DynamicBlockReferencePropertyCollection pc = blkRef.DynamicBlockReferencePropertyCollection;

                                    // Loop through, getting the info for each property
                                    if (!blkRef.IsWriteEnabled)
                                        blkRef.UpgradeOpen();
                                    foreach (DynamicBlockReferenceProperty prop in pc)
                                    {

                                        if (prop.PropertyName == pname)
                                        {
                                           
                                            prop.Value = pvalue;
                                        }

                                    }
                                    blkRef.DowngradeOpen();
                                }//
                            }
                        }
                    }
                    tr.Commit();
                }

            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
            }
        }
        [CommandMethod("InsertSwitch", "swo", CommandFlags.Modal | CommandFlags.Redraw)]
        public static void InsertSwitchBlock()
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            // db.ObjectAppended += new ObjectEventHandler(dbase_ObjectAppended);//removed by fail

            Editor ed = doc.Editor;

            string blkname = "switch";

            BlockReference bref = null;
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false);

                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false);

                    BlockTableRecord blk = (BlockTableRecord)tr.GetObject(bt[blkname], OpenMode.ForRead, false);

                    Point3d ip = ed.GetPoint("\nPick insertion point: ").Value;

                    Plane plan = new Plane(Point3d.Origin, Vector3d.ZAxis);

                     bref = new BlockReference(ip, blk.ObjectId);

                    bref.BlockUnit = UnitsValue.Inches;

                    bref.Rotation = 0;

                    bref.ScaleFactors = new Scale3d(1.0);

                    btr.AppendEntity(bref);

                    tr.AddNewlyCreatedDBObject(bref, true);

                    SetAttributes(db, tr, bref);

                   //UpdateSwitch(db, bref, "Visibility", "Disconnected");

                    tr.Commit();
                }

                PromptKeywordOptions opts = new PromptKeywordOptions("Choose a Visibility? [Connected/Disconnected]", "Connected Disconnected");

                opts.AllowArbitraryInput = true;

                PromptResult res = ed.GetKeywords(opts);

                ed.WriteMessage("\nChoosen\t{0}", res.StringResult);

                string choose = res.StringResult;

                UpdateSwitch(db, bref, "Visibility", choose);

            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\n{0}\n{1}", ex.Message, ex.StackTrace);
            }
            finally
            {
               // db.ObjectAppended -= new ObjectEventHandler(dbase_ObjectAppended);//removed by fail
            }
        }


        public static void SetAttributes(Database db, Transaction tr, BlockReference bref)
        {
            BlockTableRecord btrec = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;

            if (btrec.HasAttributeDefinitions)
            {
                Autodesk.AutoCAD.DatabaseServices.AttributeCollection atcoll = bref.AttributeCollection;


                foreach (ObjectId subid in btrec)
                {
                    Entity ent = (Entity)subid.GetObject(OpenMode.ForRead);

                    AttributeDefinition attDef = ent as AttributeDefinition;


                    if (attDef != null)
                    {
                        AttributeReference attRef = new AttributeReference();

                        attRef.SetDatabaseDefaults();

                        attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);

                        attRef.Position = attDef.Position.TransformBy(bref.BlockTransform);

                        attRef.Tag = attDef.Tag;

                        attRef.TextString = attDef.TextString;

                        attRef.AdjustAlignment(db);

                        atcoll.AppendAttribute(attRef);

                        tr.AddNewlyCreatedDBObject(attRef, true);

                    }

                }
            }
        }

~'J'~

Inserted this code in visual studio and the following errors were generated

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: BLOCKHEADS: Managing Dynamic Block Refs' Visibility State
« Reply #20 on: May 07, 2012, 05:23:34 AM »
Hi,

The code you pasted in VS defines methods.
Methods must be defined within a class which may be defined within a namespace.

It looks like you need to learn .NET (and AutoCAD .NET) basics before copying and pasting code...
Have a look here:
http://www.theswamp.org/index.php?topic=32381.0
Speaking English as a French Frog

serga

  • Guest
Re: BLOCKHEADS: Managing Dynamic Block Refs' Visibility State
« Reply #21 on: May 07, 2012, 06:45:46 AM »
ok, thanks