TheSwamp

Code Red => .NET => Topic started by: hobleglobsquishyworms on June 14, 2019, 12:39:37 AM

Title: Is external drawing a dynamic block
Post by: hobleglobsquishyworms on June 14, 2019, 12:39:37 AM
2 parter:

1) One little trick with dynamic blocks to store them is to create it in one drawing then use the wblock command to save it out as its own drawing (see image). 

2) Anyone know a way I can determine if a drawing is a dynamic block like this before importing through .net / autocad API?  When I import it using the following code it inserts it as a typical block with no dynamic functionality.  I have to explode it.   

Thanks for any input / advice you can give!

Code: [Select]
        public void blocktry()
        {
            Insert.Enabled = false;
            Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
            Editor ed = acDoc.Editor;

            using (DocumentLock lockmedoc = acDoc.LockDocument())
            {
                string blockName = listBox1.SelectedItem.ToString();
                string blockQualifiedFileName = path + "\\" + comboBox1.Text + "\\" + listBox1.SelectedItem.ToString() + ".dwg";

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    try
                    {
                        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                        if (!bt.Has(blockName))
                        {
                            Database tmpDb = new Database(false, true);
                            tmpDb.ReadDwgFile(blockQualifiedFileName, System.IO.FileShare.Read, true, "");
                            // add the block to the ActiveDrawing blockTable
                            try
                            {
                                db.Insert(blockName, tmpDb, true);
                            }
                            catch
                            {
                            }
                        }

                        PromptPointResult ppr = ed.GetPoint("\nSpecify insertion point: ");
                        if (ppr.Status == PromptStatus.Cancel)
                            Insert.Enabled = true;
                        if (ppr.Status != PromptStatus.OK)
                            return;

                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        BlockReference br = new BlockReference(Point3d.Origin, bt[blockName]);
                        br.ScaleFactors = new Scale3d(1);
                        br.Rotation = 0;

                        br.TransformBy(Matrix3d
                            .Displacement(ppr.Value - Point3d.Origin)
                            .PreMultiplyBy(ed.CurrentUserCoordinateSystem));

                        btr.AppendEntity(br);
                        tr.AddNewlyCreatedDBObject(br, true);
                        ed.WriteMessage("\n" + br.Name + " Inserted");
                        Insert.Enabled = true;
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception exx)
                    {
                        ed.WriteMessage("\n" + exx.ToString());
                        Insert.Enabled = true;
                    }
                    tr.Commit();
                    tr.Dispose();
                }
            }
        }


Title: Re: Is external drawing a dynamic block
Post by: hobleglobsquishyworms on June 14, 2019, 01:15:41 AM
Nevermind. Just rewrote the block insert to check for stuff. 
Title: Re: Is external drawing a dynamic block
Post by: gile on June 14, 2019, 01:17:46 AM
Hi,

Not deeply tested, but you can check if the extension dictionary of the model space block table record contains "ACAD_ENHANCEDBLOCK".
Title: Re: Is external drawing a dynamic block
Post by: hobleglobsquishyworms on June 14, 2019, 01:50:09 AM
Hi,

Not deeply tested, but you can check if the extension dictionary of the model space block table record contains "ACAD_ENHANCEDBLOCK".

Thanks :)   :idea: