Открыть новый шаблон, если в текущем шаблоне есть блоки / блоки при вставке файла DWG - PullRequest
0 голосов
/ 24 июня 2019

Я пытаюсь вставить файл DWG в пустой шаблон. Сначала выполняется вставка файла DWG, но когда пользователь пытается добавить другой файл DWG, мне нужно открыть новый шаблон и вставить эту DWG.

Это функция вставки, которая работает при открытом пустом шаблоне:

public void InsertDrawingAsBlock(Document doc, string path, string blockname, Point3d ipt)
    {
        try
        {
            Database curdb = doc.Database;
            Editor ed = doc.Editor;
            DocumentLock loc = doc.LockDocument();
            using (loc)
            {
                ObjectId blkid = ObjectId.Null;

                Database db = new Database(false, true);

                using (db)
                {
                    db.ReadDwgFile(path, System.IO.FileShare.Read, true, "");

                    blkid = curdb.Insert(path, db, true);

                    using (Transaction tr = doc.TransactionManager.StartTransaction())
                    {
                        BlockTable bt = (BlockTable)tr.GetObject(curdb.BlockTableId, OpenMode.ForRead);
                        if (bt.Has(blockname))
                        {
                            System.Windows.Forms.MessageBox.Show(string.Format("Block {0} does already exist\nTry another block or Exit", blockname));

                            return;
                        }
                        bt.UpgradeOpen();

                        BlockTableRecord btrec = (BlockTableRecord)blkid.GetObject(OpenMode.ForRead);
                        btrec.UpgradeOpen();

                        btrec.Name = blockname;
                        btrec.DowngradeOpen();
                        foreach (ObjectId index in btrec)
                        {
                            Entity en = (Entity)index.GetObject(OpenMode.ForRead);

                            AttributeDefinition adef = en as AttributeDefinition;

                            if (adef != null)
                            {
                                ed.WriteMessage("\n" + adef.Tag);
                            }
                        }//<--- debug only

                        BlockTableRecord btr = (BlockTableRecord)curdb.CurrentSpaceId.GetObject(OpenMode.ForWrite);

                        using (btr)
                        {
                            using (BlockReference bref = new BlockReference(ipt, blkid))
                            {
                                Matrix3d mat = Matrix3d.Identity;

                                bref.TransformBy(mat);

                                bref.ScaleFactors = new Scale3d(1, 1, 1);

                                btr.AppendEntity(bref);

                                tr.AddNewlyCreatedDBObject(bref, true);

                                // To Explode the Block. * START.
                                using (DBObjectCollection dbObjctColcton = new DBObjectCollection())
                                {
                                    bref.Explode(dbObjctColcton);
                                    foreach (DBObject dbObj in dbObjctColcton)
                                    {
                                        Entity acEnt = dbObj as Entity;

                                        btr.AppendEntity(acEnt);
                                        tr.AddNewlyCreatedDBObject(dbObj, true);

                                        acEnt = tr.GetObject(dbObj.ObjectId, OpenMode.ForWrite) as Entity;
                                    }
                                }
                                // To Explode the Block. * END.

                                using (BlockTableRecord btAttRec = (BlockTableRecord)bref.BlockTableRecord.GetObject(OpenMode.ForRead))
                                {
                                    Autodesk.AutoCAD.DatabaseServices.AttributeCollection atcoll = bref.AttributeCollection;

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

                                        AttributeDefinition attDef = ent as AttributeDefinition;

                                        if (attDef != null)
                                        {
                                            // ed.WriteMessage("\nValue: " + attDef.TextString);
                                            AttributeReference attRef = new AttributeReference();

                                            attRef.SetPropertiesFrom(attDef);

                                            attRef.Visible = attDef.Visible;


                                            attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);
                                            attRef.HorizontalMode = attDef.HorizontalMode;

                                            attRef.VerticalMode = attDef.VerticalMode;

                                            attRef.Rotation = attDef.Rotation;

                                            attRef.TextStyleId = attDef.TextStyleId;

                                            attRef.Position = attDef.Position + ipt.GetAsVector();
                                            attRef.Tag = attDef.Tag;

                                            attRef.FieldLength = attDef.FieldLength;

                                            attRef.TextString = attDef.TextString;

                                            attRef.AdjustAlignment(curdb);//?

                                            atcoll.AppendAttribute(attRef);

                                            tr.AddNewlyCreatedDBObject(attRef, true);
                                        }

                                    }

                                }                              
                                bref.BlockUnit = UnitsValue.Inches;
                                bref.DowngradeOpen();
                            }
                        }

                        btrec.DowngradeOpen();
                        bt.DowngradeOpen();
                        ed.Regen();
                        tr.Commit();
                    }
                }
            }

            }
            catch
            {              Autodesk.AutoCAD.ApplicationServices.Core.Application.ShowAlertDialog("No Templete or DWG is Open!");
            }
            finally
            {
                //
            }   
    }

Также я нашел, как открыть новый пустой шаблон:

public static void NewDrawing()
{
    string strTemplatePath = "acad.dwt";

    DocumentCollection acDocMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
    Document acDoc = acDocMgr.Add(strTemplatePath);

    acDocMgr.MdiActiveDocument = acDoc;
}

Возможно, разработчик плагинов для Pro-AutoCAD довольно прост, но AutoCAD-API является новым для меня, и я очень ценю вашу помощь.

...