我写的代码每次添加2个相同的块后,如果把光标移到第一个块上就会出现错误; "致命错误:unhandled Access Violation Reading 0x0000 Exception at 64906b98h"望高手帮忙解决下。我把我的代码放在下面,大家帮忙看看,谢谢大家了。急等答案。 /// <summary> /// 模板空间中插入图元 /// </summary> public void InsertBlock(string BlockName,string OBJName) { try { ObjectId objectid = new ObjectId(); Database db = new Database(); db.ReadDwgFile(BlockName, System.IO.FileShare.None, true, ""); db.CloseInput(true); //建立属性块 AttributeReference AttRef = new AttributeReference(); using (Transaction tran = db.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)tran.GetObject(db.BlockTableId, OpenMode.ForRead); SymbolUtilityServices s = new SymbolUtilityServices(); OBJName = s.GetBlockNameFromInsertPathName(OBJName); //OBJName=db.gett if (bt.Has(OBJName)) { objectid = bt[OBJName]; } //遍历块查找属性 BlockTableRecord BlockBtr = (BlockTableRecord)tran.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead); if (BlockBtr.HasAttributeDefinitions) { foreach (ObjectId id in BlockBtr) { Entity entity = (Entity)tran.GetObject(id, OpenMode.ForRead, false); if (entity is AttributeDefinition) { AttributeDefinition AttDef = ((AttributeDefinition)(entity)); AttRef.SetPropertiesFrom(AttDef); //AttRef.Position = new Point3d(AttDef.Position.X + brf.Position.X, AttDef.Position.Y + brf.Position.Y, AttDef.Position.Z + brf.Position.Z); AttRef.Height = AttDef.Height; AttRef.Rotation = AttDef.Rotation; AttRef.Tag = AttDef.Tag; AttRef.TextString = AttDef.TextString; } } } tran.Commit(); } Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; PromptPointOptions ppo = new PromptPointOptions("选择插入点"); PromptPointResult pps; pps = ed.GetPoint(ppo); Point3d pt3d; if (pps.Status == PromptStatus.OK) { pt3d = pps.Value; } Database database = HostApplicationServices.WorkingDatabase; using (Transaction tr = database.TransactionManager.StartTransaction()) { BlockTable bt1 = (BlockTable)tr.GetObject(database.BlockTableId, OpenMode.ForRead, false); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt1[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false); BlockReference brf = new BlockReference(pt3d, objectid); btr.AppendEntity(brf); //tr.AddNewlyCreatedDBObject(brf, true); #region 加入块属性 //btr.AppendEntity(brf); brf.AttributeCollection.AppendAttribute(AttRef); tr.AddNewlyCreatedDBObject(AttRef,true); tr.AddNewlyCreatedDBObject(brf,true); #endregion tr.Commit(); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { MessageBox.Show(ex.Message); } } /// <summary> /// 得到数据库对象 /// </summary> /// <param name="fileName"></param> /// <returns></returns> private Database GetDatabaseFromFile(string fileName) { Database databaseFromFile = new Database(false, true); databaseFromFile.ReadDwgFile(fileName, System.IO.FileShare.None, false, ""); databaseFromFile.CloseInput(true); return databaseFromFile; } /// <summary> /// 启动事件调用添加图块方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pictureBox2_Click(object sender, EventArgs e) { this.Hide(); this.InsertBlock("F:\\AutoCADTest\\AutoCAD\\TestWindows\\DWG\\_HL-_soar_4.dwg", "SYMB1_S_73"); this.Dispose(); } |