如何加入dwg参照。成功!
本帖最后由 作者 于 2009-9-28 15:50:31 编辑 <br /><br /> <p>现在有1000多张图,存在一个文件夹里面,没有图框,需要给他们全部加入一个dwg参照作为图框,插入坐标为0,0,0,不知道如何实现?</p><p>谢谢!</p><p>using (Transaction operTrans = operDb.TransactionManager.StartTransaction())<br/> {</p><p> BlockTable operBlk;<br/> operBlk = (BlockTable)operTrans.GetObject(operDb.BlockTableId, OpenMode.ForWrite);<br/> BlockTableRecord btr = (BlockTableRecord)operTrans.GetObject(operBlk, OpenMode.ForWrite);<br/> //插入外部参照<br/> <font color="#f73809">ObjectId id = operDb.AttachXref(@".\图框.dwg", "图框"); //这样插入的还是绝对路径!</font></p><p><font color="#f73809"> ObjectId id = operDb.AttachXref(".\\图框.dwg", "图框"); //这样插入的还是相对路径!<br/></font> BlockReference blkref = new BlockReference(Point3d.Origin, id);<br/> btr.AppendEntity(blkref);<br/> operTrans.AddNewlyCreatedDBObject(blkref, true);</p><p> //提交事务<br/> operTrans.Commit();<br/> }</p><p>怎样插入相对路径呢?是不是要设置系统变量之类的?</p> <p>遍历文件</p><p>Database db = new Database();</p><p>db.ReadDwgFile(...)读入文件</p><p>插入图框定义(db.Insert或Wblock)</p><p>插入块参照(BlockReference)</p><p>db.Save()保存文件</p> 那我要读入dxf文件怎么办呢?<p>db.ReadDwgFile(...)读入文件也可以读吗?</p> <pre class="Element100"><strong>public</strong> <strong>void</strong> <strong>DxfIn</strong>(<br/> <strong>string</strong> <strong>fileName</strong>, <br/> <strong>string</strong> <strong>logFilename</strong><br/>);</pre> 没明白意思啊<strong>string</strong> <strong>fileName</strong>, <br/> <strong>string</strong> <strong>logFilename</strong><br/>分别作什么用的啊? <p>文档的解释</p><p class="Element10">This function reads the DXF file specified into the database object. </p><p class="Element10">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. </p><p class="Element10"><strong>Warning</strong> </p><p class="Element10">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.</p> 本帖最后由 作者 于 2009-9-26 18:06:47 编辑 <br /><br /> <p>frameDb = new Database(false, false);//新建图框数据库</p><p>//frameDb.DxfIn(operFilePath, "");</p><p>frameDb.ReadDwgFile(operFilePath, Autodesk.AutoCAD.DatabaseServices.FileOpenMode.OpenForReadAndWriteNoShare, true, "");</p><p>执行第3句正常,注释掉弟3句执行第2句就报错,内存出错,不知道什么原因?</p><p>执行第2句的operFilePath是dxf文件的,执行第3句的operFilePath是dwg文件的</p><p></p><p></p>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);
} 本帖最后由 作者 于 2009-9-26 22:11:39 编辑 <br /><br /> <p>多谢斑竹,我这就试试!多谢多谢!</p><p>试验之后,出现错误</p><p>operDb = new Database(false, true);//新建操作数据库<br/> if (fileType == "DWG")//分为DWG,DXF分类打开图形数据库<br/> {<br/> operDb.ReadDwgFile(operFilePath, FileOpenMode.OpenForReadAndWriteNoShare, true, "");<br/> }<br/> else<br/> {<br/> operDb.DxfIn(operFilePath, null);<br/> }<br/> using(Transaction operTrans=operDb.TransactionManager.StartTransaction())<br/> {<br/> //新建块定义,外部参照<br/> BlockTable operBlk;<br/> operBlk = (BlockTable)operTrans.GetObject(operDb.BlockTableId, OpenMode.ForWrite);<br/> BlockTableRecord operRec = new BlockTableRecord();<br/> operRec.PathName = framePath;<br/> operRec.Origin = new Point3d(0, 0, 0);<br/> operBlk.Add(operRec);<br/> operTrans.AddNewlyCreatedDBObject(operRec, true);<br/> //新建圆<br/> BlockTableRecord btr = (BlockTableRecord)operTrans.GetObject(operBlk, OpenMode.ForWrite);<br/> Circle acCirc = new Circle();<br/> acCirc.SetDatabaseDefaults();<br/> acCirc.Center = new Point3d(2, 3, 0);<br/> acCirc.Radius = 400;<br/> btr.AppendEntity(acCirc);<br/> operTrans.AddNewlyCreatedDBObject(acCirc, true);</p><p> operTrans.Commit();<br/> }<br/> operDb.SaveAs(operFilePath, null);</p><p>执行到保存这一句是CAD弹出消息框,说保存错误。但程序继续执行完成,打开修改后的图形,圆已经加入,但外部参照没有加入,而全选就出错,退出CAD。</p><p>将保存改成operDb.SaveAs(operFilePath,DwgVersion.AC1015);则程序运行到这一句出现异常,无法继续。</p><p>不知如何处理?还有我的外部参照有什么问题吗?应该如何添加呢?</p> <p>Database Method</p><p>public ObjectId AttachXref(<br/> string fileName, <br/> string blockName<br/>);<br/>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. </p><p>This function does not lock the document, nor does it create an BlockReference instance of the new block table record.<br/></p>
页:
[1]
2