Esempio n. 1
0
        public static void InsertBlock(string fileName)
        {
            //块ID
            ObjectId blockId;
            //图形数据库读取外部图块
            Database blockDatabase = new Database(false, true);

            blockDatabase.ReadDwgFile(fileName, System.IO.FileShare.Read, false, string.Empty);
            // blockDatabase.CloseInput(true);

            Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt        = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
                string     blockName = SymbolUtilityServices.GetBlockNameFromInsertPathName(fileName);
                //将外部图块插入到当前模型空间
                blockId = db.Insert(blockName, blockDatabase, true);
                trans.Commit();
            }

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable       bt  = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                //通过块定义创建块参照
                BlockReference br = new BlockReference(new Point3d(0, 0, 0), blockId);
                //把块参照添加到块表记录
                btr.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);
                tr.Commit();
            }
        }
Esempio n. 2
0
        public void InserirBlocoNoDesenho(string nomeArquivo)
        {
            Document acDoc     = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database currDwgDb = acDoc.Database;
            Editor   acDocEd   = acDoc.Editor;

            try
            {
                using (DocumentLock docLock = acDoc.LockDocument())
                {
                    using (Database xDb = new Database(false, true))
                    {
                        xDb.ReadDwgFile(nomeArquivo, System.IO.FileShare.Read, true, "");
                        using (Transaction tr = acDoc.TransactionManager.StartTransaction())
                        {
                            string   name = SymbolUtilityServices.GetBlockNameFromInsertPathName(nomeArquivo);
                            ObjectId id   = currDwgDb.Insert(name, xDb, true);

                            if (id.IsNull)
                            {
                                acDocEd.WriteMessage("Failed to insert block");
                                return;
                            }
                            BlockTableRecord   currSpace = (BlockTableRecord)(tr.GetObject(currDwgDb.CurrentSpaceId, OpenMode.ForWrite));
                            PromptPointResult  pPtRes;
                            PromptPointOptions pPtOpts = new PromptPointOptions("");
                            //Escolher o ponto de inserção
                            pPtOpts.Message = "\nEscolha o ponto de inserção do insumo: ";
                            pPtRes          = acDoc.Editor.GetPoint(pPtOpts);
                            Point3d            p3d    = pPtRes.Value;
                            CoordinateSystem3d coordS = new CoordinateSystem3d(p3d, currDwgDb.Ucsxdir, currDwgDb.Ucsydir);
                            BlockReference     insert = new BlockReference(p3d, id);
                            insert.Normal = coordS.Zaxis;
                            currSpace.AppendEntity(insert);
                            insert.SetDatabaseDefaults();
                            tr.AddNewlyCreatedDBObject(insert, true);
                            tr.Commit();
                        }
                    }
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                acDocEd.WriteMessage("\nError during copy: " + ex.Message);
            }
        }
Esempio n. 3
0
        public void InserirBlocoNoDesenho(string nomeArquivo)
        {
            Document acDoc     = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database currDwgDb = acDoc.Database;
            Editor   acDocEd   = acDoc.Editor;

            try
            {
                using (DocumentLock docLock = acDoc.LockDocument())
                {
                    using (Database xDb = new Database(false, true))
                    {
                        xDb.ReadDwgFile(nomeArquivo, System.IO.FileShare.Read, true, "");
                        using (Transaction tr = acDoc.TransactionManager.StartTransaction())
                        {
                            string   name = SymbolUtilityServices.GetBlockNameFromInsertPathName(nomeArquivo);
                            ObjectId id   = currDwgDb.Insert(name, xDb, true);

                            if (id.IsNull)
                            {
                                acDocEd.WriteMessage("Failed to insert block");
                                return;
                            }
                            BlockTableRecord   currSpace = (BlockTableRecord)(tr.GetObject(currDwgDb.CurrentSpaceId, OpenMode.ForWrite));
                            PromptPointResult  pPtRes;
                            PromptPointOptions pPtOpts = new PromptPointOptions("");
                            //Escolher o ponto de inserção
                            pPtOpts.Message = "\nEscolha o ponto de inserção do insumo: ";
                            pPtRes          = acDoc.Editor.GetPoint(pPtOpts);
                            Point3d            p3d    = pPtRes.Value;
                            CoordinateSystem3d coordS = new CoordinateSystem3d(p3d, currDwgDb.Ucsxdir, currDwgDb.Ucsydir);
                            BlockReference     insert = new BlockReference(p3d, id);
                            insert.Normal = coordS.Zaxis;

                            //Dictionary<string, string> attValues = new Dictionary<string, string>();
                            //attValues.Add("ATT1", "foo");
                            //
                            //RXClass attDefClass = RXClass.GetClass(typeof(AttributeDefinition));
                            //foreach (ObjectId oid in currSpace)
                            //{
                            //    if (id.ObjectClass != attDefClass)
                            //        continue;
                            //    AttributeDefinition attDef = (AttributeDefinition)tr.GetObject(oid, OpenMode.ForRead);
                            //    AttributeReference attRef = new AttributeReference();
                            //    attRef.SetAttributeFromBlock(attDef, insert.BlockTransform);
                            //    if (attValues != null && attValues.ContainsKey(attDef.Tag.ToUpper()))
                            //    {
                            //        attRef.TextString = attValues[attDef.Tag.ToUpper()];
                            //    }
                            //    insert.AttributeCollection.AppendAttribute(attRef);
                            //    tr.AddNewlyCreatedDBObject(attRef, true);
                            //}

                            currSpace.AppendEntity(insert);
                            insert.SetDatabaseDefaults();
                            tr.AddNewlyCreatedDBObject(insert, true);
                            tr.Commit();
                        }
                    }
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                acDocEd.WriteMessage("\nError during copy: " + ex.Message);
            }
        }