gsteven 发表于 2009-9-25 22:46:00

如何加入dwg参照。成功!

现在有1000多张图,存在一个文件夹里面,没有图框,需要给他们全部加入一个dwg参照作为图框,插入坐标为0,0,0,不知道如何实现?
谢谢!
using (Transaction operTrans = operDb.TransactionManager.StartTransaction())
                  {
                        BlockTable operBlk;
                        operBlk = (BlockTable)operTrans.GetObject(operDb.BlockTableId, OpenMode.ForWrite);
                        BlockTableRecord btr = (BlockTableRecord)operTrans.GetObject(operBlk, OpenMode.ForWrite);
                        //插入外部参照
                        ObjectId id = operDb.AttachXref(@".\图框.dwg", "图框"); //这样插入的还是绝对路径!
                        ObjectId id = operDb.AttachXref(".\\图框.dwg", "图框"); //这样插入的还是相对路径!
                        BlockReference blkref = new BlockReference(Point3d.Origin, id);
                        btr.AppendEntity(blkref);
                        operTrans.AddNewlyCreatedDBObject(blkref, true);
                        //提交事务
                        operTrans.Commit();
                  }
怎样插入相对路径呢?是不是要设置系统变量之类的?

雪山飞狐_lzh 发表于 2009-9-25 23:00:00

遍历文件
Database db = new Database();
db.ReadDwgFile(...)读入文件
插入图框定义(db.Insert或Wblock)
插入块参照(BlockReference)
db.Save()保存文件

gsteven 发表于 2009-9-26 14:12:00

那我要读入dxf文件怎么办呢?db.ReadDwgFile(...)读入文件也可以读吗?

雪山飞狐_lzh 发表于 2009-9-26 15:53:00

publicvoidDxfIn(
    stringfileName,
    stringlogFilename
);

gsteven 发表于 2009-9-26 15:56:00

没明白意思啊stringfileName,
    stringlogFilename
分别作什么用的啊?

雪山飞狐_lzh 发表于 2009-9-26 16:01:00

文档的解释
This function reads the DXF file specified into the database object.
The default for fileName is no output file. When there is no output file, warning/error messages will be output character-by-character through the displayChar() method in HostApplicationServices.
Warning
This function should be used only on a newly created Database that was created with its constructor's buildDefaultDrawing argument set to false. If this method is used on an Database created with buildDefaultDrawing set to true or a Database that already has information in it (for any reason including a previous call to this method), then memory leaks or possibly fatal errors will result.

gsteven 发表于 2009-9-26 17:03:00


frameDb = new Database(false, false);//新建图框数据库
//frameDb.DxfIn(operFilePath, "");
frameDb.ReadDwgFile(operFilePath, Autodesk.AutoCAD.DatabaseServices.FileOpenMode.OpenForReadAndWriteNoShare, true, "");
执行第3句正常,注释掉弟3句执行第2句就报错,内存出错,不知道什么原因?
执行第2句的operFilePath是dxf文件的,执行第3句的operFilePath是dwg文件的

雪山飞狐_lzh 发表于 2009-9-26 18:07:00


      
      public static void tt2()
      {
            Database db = new Database(false, true);
            db.DxfIn("D:\\1.dxf", null);
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                DBPoint dptest = new DBPoint(Point3d.Origin);
                btr.AppendEntity(dptest);
                tr.AddNewlyCreatedDBObject(dptest, true);
                tr.Commit();
            }
            db.SaveAs("d:\\1.dwg", null);
      }

gsteven 发表于 2009-9-26 18:10:00


多谢斑竹,我这就试试!多谢多谢!
试验之后,出现错误
operDb = new Database(false, true);//新建操作数据库
                  if (fileType == "DWG")//分为DWG,DXF分类打开图形数据库
                  {
                        operDb.ReadDwgFile(operFilePath, FileOpenMode.OpenForReadAndWriteNoShare, true, "");
                  }
                  else
                  {
                        operDb.DxfIn(operFilePath, null);
                  }
                  using(Transaction operTrans=operDb.TransactionManager.StartTransaction())
                  {
                        //新建块定义,外部参照
                        BlockTable operBlk;
                        operBlk = (BlockTable)operTrans.GetObject(operDb.BlockTableId, OpenMode.ForWrite);
                        BlockTableRecord operRec = new BlockTableRecord();
                        operRec.PathName = framePath;
                        operRec.Origin = new Point3d(0, 0, 0);
                        operBlk.Add(operRec);
                        operTrans.AddNewlyCreatedDBObject(operRec, true);
                        //新建圆
                        BlockTableRecord btr = (BlockTableRecord)operTrans.GetObject(operBlk, OpenMode.ForWrite);
                        Circle acCirc = new Circle();
                        acCirc.SetDatabaseDefaults();
                        acCirc.Center = new Point3d(2, 3, 0);
                        acCirc.Radius = 400;
                        btr.AppendEntity(acCirc);
                        operTrans.AddNewlyCreatedDBObject(acCirc, true);
                        operTrans.Commit();
                  }
                  operDb.SaveAs(operFilePath, null);
执行到保存这一句是CAD弹出消息框,说保存错误。但程序继续执行完成,打开修改后的图形,圆已经加入,但外部参照没有加入,而全选就出错,退出CAD。
将保存改成operDb.SaveAs(operFilePath,DwgVersion.AC1015);则程序运行到这一句出现异常,无法继续。
不知如何处理?还有我的外部参照有什么问题吗?应该如何添加呢?

雪山飞狐_lzh 发表于 2009-9-26 23:03:00

Database Method
public ObjectId AttachXref(
    string fileName,
    string blockName
);
Attaches the xref file specified by fileName to the database, thus creating a new xref BlockTableRecord. The new block table record's name is specified in blockName. Its object ID is returned.
This function does not lock the document, nor does it create an BlockReference instance of the new block table record.
页: [1]
查看完整版本: 如何加入dwg参照。成功!