C#对AUTOCAD二次开发插入图元的问题,求高手指点
我使我使用c#对AUTOCAD进行二次开发,吧已经做好的DWG插入到模板空间中,插入的位置由鼠标来选择 <br/> 我本来想用-INSERT命令的,但是调用后发现输入命令和块名称后要点击下鼠标右键,我在块名称后面直接空格都无法执行回车命令。。。。。。这个很郁闷,不知道如何解决。 <br/> 后来我又发现了一个类叫blockreference但是不太会用这个类。 <br/>希望高手给点提示或者实例代码。我的目的就是想把DWG插入到模板空间中去,因本人菜鸟刚刚接触AUTOCAD二次开发。求高手指点。 <p></p> <p>先调用Database的Insert方法把文件插入快表,然后new BlockReference</p><p></p><p>//下面的代码是我用在自定义类里的,要自己改下:)</p><p> public void GetBlockdefFromFile(string filename, bool over)<br/> {<br/> try<br/> {<br/> FileInfo fi = new FileInfo(filename);<br/> string blkdefname = fi.Name;<br/> if (blkdefname.Contains("."))<br/> {<br/> blkdefname = blkdefname.Substring(0, blkdefname.LastIndexOf('.'));<br/> }</p><p> bool has = m_BlockTable.Has(blkdefname);<br/> if ((has && over) || !has)<br/> {<br/> //using (DocumentLock doclock = Application.DocumentManager.MdiActiveDocument.LockDocument())<br/> //{<br/> Database db = new Database();<br/> db.ReadDwgFile(filename, FileShare.Read, true, null);<br/> m_Database.Insert(BlockTableRecord.ModelSpace, blkdefname, db, false);<br/> //}<br/> }<br/> }<br/> catch <br/> { }<br/> }</p> <p>我写的代码每次添加2个相同的块后,如果把光标移到第一个块上就会出现错误;</p><p>"致命错误:unhandled Access Violation Reading 0x0000 Exception at 64906b98h"望高手帮忙解决下。我把我的代码放在下面,大家帮忙看看,谢谢大家了。急等答案。</p><p> /// <summary><br/> /// 模板空间中插入图元<br/> /// </summary><br/> public void InsertBlock(string BlockName,string OBJName)<br/> { <br/> try<br/> {<br/> ObjectId objectid = new ObjectId();<br/> Database db = new Database();<br/> db.ReadDwgFile(BlockName, System.IO.FileShare.None, true, "");<br/> db.CloseInput(true);<br/> //建立属性块<br/> AttributeReference AttRef = new AttributeReference();<br/> using (Transaction tran = db.TransactionManager.StartTransaction())<br/> {<br/> BlockTable bt = (BlockTable)tran.GetObject(db.BlockTableId, OpenMode.ForRead);<br/> <br/> SymbolUtilityServices s = new SymbolUtilityServices();</p><p> OBJName = s.GetBlockNameFromInsertPathName(OBJName);<br/> //OBJName=db.gett<br/> if (bt.Has(OBJName))<br/> {<br/> objectid = bt;<br/> }<br/> //遍历块查找属性<br/> BlockTableRecord BlockBtr = (BlockTableRecord)tran.GetObject(bt, OpenMode.ForRead);<br/> if (BlockBtr.HasAttributeDefinitions)<br/> {<br/> foreach (ObjectId id in BlockBtr)<br/> {<br/> Entity entity = (Entity)tran.GetObject(id, OpenMode.ForRead, false);<br/> if (entity is AttributeDefinition)<br/> {<br/> AttributeDefinition AttDef = ((AttributeDefinition)(entity));<br/> AttRef.SetPropertiesFrom(AttDef);<br/> //AttRef.Position = new Point3d(AttDef.Position.X + brf.Position.X, AttDef.Position.Y + brf.Position.Y, AttDef.Position.Z + brf.Position.Z);<br/> AttRef.Height = AttDef.Height;<br/> AttRef.Rotation = AttDef.Rotation;<br/> AttRef.Tag = AttDef.Tag;<br/> AttRef.TextString = AttDef.TextString;<br/> }<br/> }<br/> }<br/> tran.Commit();<br/> }</p><p> <br/> Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;</p><p> </p><p> PromptPointOptions ppo = new PromptPointOptions("选择插入点");<br/> PromptPointResult pps;</p><p> pps = ed.GetPoint(ppo);<br/> Point3d pt3d;<br/> if (pps.Status == PromptStatus.OK)<br/> {<br/> pt3d = pps.Value;<br/> }<br/> Database database = HostApplicationServices.WorkingDatabase;</p><p> using (Transaction tr = database.TransactionManager.StartTransaction())<br/> {</p><p> BlockTable bt1 = (BlockTable)tr.GetObject(database.BlockTableId, OpenMode.ForRead, false);<br/> BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt1, OpenMode.ForWrite, false);<br/> <br/> BlockReference brf = new BlockReference(pt3d, objectid);</p><p> btr.AppendEntity(brf);</p><p> //tr.AddNewlyCreatedDBObject(brf, true);</p><p> #region 加入块属性 <br/> <br/> //btr.AppendEntity(brf);<br/> brf.AttributeCollection.AppendAttribute(AttRef);<br/> tr.AddNewlyCreatedDBObject(AttRef,true);<br/> tr.AddNewlyCreatedDBObject(brf,true);<br/> #endregion</p><p> tr.Commit();<br/> }<br/> <br/> <br/> <br/> <br/> }<br/> catch (Autodesk.AutoCAD.Runtime.Exception ex)<br/> {<br/> MessageBox.Show(ex.Message);<br/> }<br/> }</p><p></p><p> /// <summary><br/> /// 得到数据库对象<br/> /// </summary><br/> /// <param name="fileName"></param><br/> /// <returns></returns><br/> private Database GetDatabaseFromFile(string fileName)<br/> {<br/> Database databaseFromFile = new Database(false, true);<br/> databaseFromFile.ReadDwgFile(fileName, System.IO.FileShare.None, false, "");<br/> databaseFromFile.CloseInput(true);<br/> return databaseFromFile;<br/> }</p><p></p><p> /// <summary><br/> /// 启动事件调用添加图块方法<br/> /// </summary><br/> /// <param name="sender"></param><br/> /// <param name="e"></param><br/> private void pictureBox2_Click(object sender, EventArgs e)<br/> {<br/> this.Hide();<br/> <br/> this.InsertBlock("F:\\AutoCADTest\\AutoCAD\\TestWindows\\DWG\\_HL-_soar_4.dwg", "SYMB1_S_73");<br/> this.Dispose();<br/> }</p> 试试这个?public bool AppendAttribRefToBlockRef(ObjectId blockdefid, ObjectId blockrefid, List<string> attribs)
{
try
{
BlockTableRecord blkdef = (BlockTableRecord)m_Transaction.GetObject(blockdefid, OpenMode.ForRead);
int i = 0;
if (blkdef.HasAttributeDefinitions)
{
BlockReference blkref = (BlockReference)m_Transaction.GetObject(blockrefid, OpenMode.ForWrite);
foreach (ObjectId id in blkdef)
{
DBObject ent = GetObject(id, OpenMode.ForRead);
if (ent.GetType() == typeof(AttributeDefinition))
{
AttributeDefinition attdef = (AttributeDefinition)ent;
AttributeReference attref = new AttributeReference();
attref.SetAttributeFromBlock(attdef, blkref.BlockTransform);
if (attdef.Justify == AttachmentPoint.BaseLeft)
attref.Position = attdef.Position.TransformBy(blkref.BlockTransform);
else
attref.AlignmentPoint = attdef.AlignmentPoint.TransformBy(blkref.BlockTransform);
if (attref.IsMTextAttribute)
attref.UpdateMTextAttribute();
if (i < attribs.Count)
attref.TextString = attribs;
else
attref.TextString = attdef.TextString;
blkref.AttributeCollection.AppendAttribute(attref);
m_Transaction.AddNewlyCreatedDBObject(attref, true);
}
}
}
return true;
}
catch
{ }
return false;
}
2006 下
db.ReadDwgFile(filename, FileShare.Read, true, null);
会在关闭文档后报unhandled Access Violation Reading 的错误
改成db.ReadDwgFile(filename, FileShare.Read, false, "");
就不报了
页:
[1]