- 积分
- 139
- 明经币
- 个
- 注册时间
- 2013-10-31
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
如题,我用drawjig从外部一个特定文件读入特定block插入到当前文档,有时能动态显示图块,大部分时候不显示,只是一个十字光标!是不是代码有问题呢?求教高人,多谢!
vs2005 c# + cad2007- public class InsertBlk : DrawJig
- {
- private BlockReference newBlkRef;
- private Point3d mCenterPt;
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- //-----------------------------------
- public void InsertBlock(string strFilename, string strBlkname)
- {
- mCenterPt = new Point3d(0, 0, 0);
- Database db = HostApplicationServices.WorkingDatabase;
- Matrix3d mt = ed.CurrentUserCoordinateSystem;
- PromptResult resJig;
- ReadBlock(strBlkname, strFilename);
- do
- {
- Transaction tr = doc.TransactionManager.StartTransaction();
- BlockTable bt = (BlockTable)tr.GetObject(this.doc.Database.BlockTableId, OpenMode.ForRead);
- this.newBlkRef = new BlockReference(mCenterPt, bt[strBlkname]);
- tr.Commit();
- this.newBlkRef.Normal = db.Ucsxdir.CrossProduct(db.Ucsydir);
- resJig = ed.Drag(this);
- if (resJig.Status == PromptStatus.OK)
- {
- AppendEntity(this.newBlkRef);
- }
- }
- while (resJig.Status != PromptStatus.Cancel);
- }
- //-----------------------------------
- private ObjectId AppendEntity(Entity ent)
- {
- ObjectId entId;
- Database db = HostApplicationServices.WorkingDatabase;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- entId = btr.AppendEntity(newBlkRef);
- trans.AddNewlyCreatedDBObject(newBlkRef, true);
- trans.Commit();
- }
- return entId;
- }
- //-----------------------------------
- private bool ReadBlock(string strBlkname, string strFilename)
- {
- using (Transaction tr = this.doc.TransactionManager.StartTransaction())
- {
- using (Database db = new Database(false, true))
- {
- db.ReadDwgFile(strFilename, System.IO.FileShare.Read, true, null);
- using (Transaction trm = db.TransactionManager.StartTransaction())
- {
- BlockTable sourceBlkTbl = (BlockTable)trm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
- if (!sourceBlkTbl.Has(strBlkname))
- {
- ed.WriteMessage("\n"+strBlkname+" does not exist in this dwg.");
- return false;
- }
- ObjectIdCollection blockIds = new ObjectIdCollection();
- blockIds.Add(sourceBlkTbl[strBlkname]);
- IdMapping IdMap = new IdMapping();
- db.WblockCloneObjects(blockIds, this.doc.Database.BlockTableId, IdMap, DuplicateRecordCloning.Replace ,false );
- }
- }
- tr.Commit();
- }
- return true;
- }
- //-----------------------------------
- protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
- {
- draw.Geometry.Draw(this.newBlkRef);
- return true;
- }
- //-----------------------------------
- protected override SamplerStatus Sampler(JigPrompts prompts)
- {
- optJigDis.UserInputControls = UserInputControls.Accept3dCoordinates;
- PromptPointResult resJigDis = prompts.AcquirePoint(optJigDis);
- Point3d tempPt = resJigDis.Value;
- if (resJigDis.Status == PromptStatus.Cancel)
- {
- return SamplerStatus.Cancel;
- }
- if (mCenterPt != tempPt)
- {
- mCenterPt = tempPt;
- this.newBlkRef.Position = mCenterPt;
- return SamplerStatus.OK;
- }
- else
- {
- return SamplerStatus.NoChange;
- }
- }
- }
该贴已经同步到 晋A青少年的微博 |
|