明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 7026|回复: 15

如何加入dwg参照。成功!

  [复制链接]
发表于 2009-9-25 22:46 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2009-9-28 15:50:31 编辑

现在有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[BlockTableRecord.ModelSpace], 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();
                    }

怎样插入相对路径呢?是不是要设置系统变量之类的?

发表于 2009-9-25 23:00 | 显示全部楼层

遍历文件

Database db = new Database();

db.ReadDwgFile(...)读入文件

插入图框定义(db.Insert或Wblock)

插入块参照(BlockReference)

db.Save()保存文件

 楼主| 发表于 2009-9-26 14:12 | 显示全部楼层
那我要读入dxf文件怎么办呢?

db.ReadDwgFile(...)读入文件也可以读吗?

发表于 2009-9-26 15:53 | 显示全部楼层
public  void  DxfIn(
    string  fileName,
    string  logFilename
);
 楼主| 发表于 2009-9-26 15:56 | 显示全部楼层
没明白意思啊string  fileName,
    string  logFilename
分别作什么用的啊?
发表于 2009-9-26 16:01 | 显示全部楼层

文档的解释

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.

 楼主| 发表于 2009-9-26 17:03 | 显示全部楼层
本帖最后由 作者 于 2009-9-26 18:06:47 编辑

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文件的

发表于 2009-9-26 18:07 | 显示全部楼层
  1.         [CommandMethod("ts2")]
  2.         public static void tt2()
  3.         {
  4.             Database db = new Database(false, true);
  5.             db.DxfIn("D:\\1.dxf", null);
  6.             using (Transaction tr = db.TransactionManager.StartTransaction())
  7.             {
  8.                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  9.                 DBPoint dptest = new DBPoint(Point3d.Origin);
  10.                 btr.AppendEntity(dptest);
  11.                 tr.AddNewlyCreatedDBObject(dptest, true);
  12.                 tr.Commit();
  13.             }
  14.             db.SaveAs("d:\\1.dwg", null);
  15.         }
 楼主| 发表于 2009-9-26 18:10 | 显示全部楼层
本帖最后由 作者 于 2009-9-26 22:11:39 编辑

多谢斑竹,我这就试试!多谢多谢!

试验之后,出现错误

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[BlockTableRecord.ModelSpace], 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);则程序运行到这一句出现异常,无法继续。

不知如何处理?还有我的外部参照有什么问题吗?应该如何添加呢?

发表于 2009-9-26 23:03 | 显示全部楼层

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.

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-18 14:56 , Processed in 0.174582 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表