Exemplo n.º 1
0
        // TODO: Add tests
        public (Database, TemplateDrawingObject) GetTemplate(Guid id)
        {
            if (Contains(id))
            {
                Database template = new Database();

                template.ReadDwgFile(Path, FileOpenMode.OpenForReadAndAllShare, false, null);
                template.CloseInput(true);

                using (Transaction trans = template.TransactionManager.StartTransaction())
                {
                    IEnumerable <BlockTableRecord> definitions = template.GetAllBlockDefinitions();
                    foreach (BlockTableRecord blockTableRecord in definitions)
                    {
                        BlockDrawingObject blockDrawingObject = new BlockDrawingObject(template);
                        blockDrawingObject.BaseObject = blockTableRecord.ObjectId;
                        if (blockDrawingObject.HasKey(TemplateDrawingObject.TEMPLATE_ID_KEY) && new Guid(blockDrawingObject[TemplateDrawingObject.TEMPLATE_ID_KEY]) == id)
                        {
                            TemplateDrawingObject templateObject = new TemplateDrawingObject(template);
                            templateObject.BaseObject = blockDrawingObject.BaseObject;
                            return(template, templateObject);
                        }
                    }
                }

                throw new InvalidOperationException("Key not found");
            }
            else
            {
                throw new InvalidOperationException("Key not recognised");
            }
        }
Exemplo n.º 2
0
        public static void BlockToTemplate()
        {
            TypedValue[] acTypValAr = new TypedValue[1];
            acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "INSERT"), 0);
            SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);

            PromptSelectionResult acSSPrompt = Application.DocumentManager.MdiActiveDocument.Editor.GetSelection(acSelFtr);
            Document currentDoc = Application.DocumentManager.MdiActiveDocument;

            if (acSSPrompt.Status == PromptStatus.OK)
            {
                if (acSSPrompt.Value.Count > 1)
                {
                    throw new ArgumentOutOfRangeException("More items than expected");
                }

                using (Transaction trans = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
                {
                    DocumentCollection acDocMgr = Application.DocumentManager;
                    Document           newDoc   = acDocMgr.Add("");
                    using (DocumentLock lockObj = newDoc.LockDocument())
                    {
                        using (Transaction destinationTrans = newDoc.TransactionManager.StartTransaction())
                        {
                            BlockReference        refObj    = (BlockReference)trans.GetObject(acSSPrompt.Value[0].ObjectId, OpenMode.ForWrite);
                            BlockRefDrawingObject reference = new BlockRefDrawingObject(currentDoc, refObj);

                            Database              source          = Application.DocumentManager.MdiActiveDocument.Database;
                            BlockDrawingObject    newInstance     = reference.GetBlock().TransferToDocument(newDoc);
                            TemplateDrawingObject blockDefinition = newInstance.ConvertToTemplate();

                            destinationTrans.Commit();
                        }
                    }

                    Application.DocumentManager.MdiActiveDocument = newDoc;
                }
            }
        }