- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 作者 于 2005-7-15 16:02:27 编辑
先建一个名为“1”的块- using System;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.ApplicationServices;
- using System.Reflection;
- using System.IO;
- using System.Collections;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.GraphicsInterface;
- namespace TlsCad
- {
- public class BlockRefJig:EntityJig
- {
- Point3d mPosition,mAnglePnt;
- Vector3d mNormal;
- double mAngle;
- int mPromptCounter;
- DynamicDimensionDataCollection m_dims;
- public BlockRefJig(Vector3d vec,ObjectId id):base(new BlockReference(new Point3d(0,0,0),id))
- {
- mPosition=new Point3d(0,0,0);
- mNormal=vec;
- mAngle=0;
- m_dims = new DynamicDimensionDataCollection();
- Dimension dim1 = new AlignedDimension();
- dim1.SetDatabaseDefaults();
- m_dims.Add(new DynamicDimensionData(dim1,true,true));
- Dimension dim2 = new AlignedDimension();
- dim2.SetDatabaseDefaults();
- m_dims.Add(new DynamicDimensionData(dim2,true,true));
- }
-
- protected override SamplerStatus Sampler(JigPrompts prompts)
- {
- JigPromptOptions jigOpts = new JigPromptOptions();
- jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NoZeroResponseAccepted | UserInputControls.NoNegativeResponseAccepted);
-
- if(mPromptCounter == 0)
- {
- jigOpts.Message = "\nInput InsertPoint:";
- PromptPointResult dres = prompts.AcquirePoint(jigOpts);
-
- Point3d positionTemp = dres.Value;
- if(positionTemp != mPosition)
- {
- mPosition = positionTemp;
- }
- else
- return SamplerStatus.NoChange;
- if(dres.Status == PromptStatus.Cancel)
- return SamplerStatus.Cancel;
- else
- return SamplerStatus.OK;
-
-
- }
- else if (mPromptCounter == 1)
- {
- jigOpts.BasePoint = mPosition;
- jigOpts.UseBasePoint = true;
- jigOpts.Message = "\nInput Angle:";
- double angleTemp = -1;
- PromptPointResult res = prompts.AcquirePoint(jigOpts);
- mAnglePnt = res.Value;
-
- angleTemp = mAnglePnt.GetVectorTo(mPosition).AngleOnPlane(
- new Plane(
- Application.DocumentManager.MdiActiveDocument.Database.Ucsorg,
- Application.DocumentManager.MdiActiveDocument.Database.Ucsxdir,
- Application.DocumentManager.MdiActiveDocument.Database.Ucsydir
- ));
- if (angleTemp != mAngle)
- mAngle = angleTemp;
- else
- return SamplerStatus.NoChange;
- if(res.Status == PromptStatus.Cancel)
- return SamplerStatus.Cancel;
- else
- return SamplerStatus.OK;
-
- }
- else
- {
- return SamplerStatus.NoChange;
- }
-
-
- }
- protected override bool Update()
- {
-
- try
- {
- ((BlockReference)Entity).Position=mPosition;
- ((BlockReference)Entity).Rotation =mAngle;
- UpdateDimensions();
-
- }
- catch(System.Exception)
- {
- return false;
- }
-
- return true;
- }
- protected override DynamicDimensionDataCollection GetDynamicDimensionData(double dimScale)
- {
- return m_dims;
- }
- protected override void OnDimensionValueChanged(Autodesk.AutoCAD.DatabaseServices.DynamicDimensionChangedEventArgs e)
- {
-
- }
- void UpdateDimensions()
- {
- BlockReference blkref = (BlockReference)Entity;
- if(mPromptCounter == 0)
- {
- AlignedDimension dim1 = (AlignedDimension)m_dims[0].Dimension;
- dim1.XLine1Point = blkref.Position;
- dim1.DimLinePoint = blkref.Position;
- }
- else
- {
- Ellipse myellipse = (Ellipse)Entity;
- AlignedDimension dim2 = (AlignedDimension)m_dims[1].Dimension;
- dim2.XLine1Point = blkref.Position;
- dim2.XLine2Point = mAnglePnt;
- dim2.DimLinePoint = blkref.Position;
- }
- }
- public void setPromptCounter(int i)
- {
- mPromptCounter = i;
- }
- public Entity GetEntity()
- {
- return Entity;
- }
- [CommandMethod("tjig")]
- static public void DoIt()
- {
- Vector3d x = Application.DocumentManager.MdiActiveDocument.Database.Ucsxdir;
- Vector3d y = Application.DocumentManager.MdiActiveDocument.Database.Ucsydir;
- Vector3d NormalVec = x.CrossProduct(y);
-
- using (TlsTM tm = new TlsTM(true))
- {
-
- BlockTable pbt=(BlockTable)tm.AutoCadTM.GetObject(tm.Database.BlockTableId, OpenMode.ForRead,true);
- BlockRefJig jig = new BlockRefJig(NormalVec,pbt["1"]);
- jig.setPromptCounter(0);
- Application.DocumentManager.MdiActiveDocument.Editor.Drag(jig);
- jig.setPromptCounter(1);
- Application.DocumentManager.MdiActiveDocument.Editor.Drag(jig);
- tm.OpenBlockTableRecord(BlockTableRecord.ModelSpace);
- tm.Add(jig.GetEntity());
- }
-
- }
- }
- public class TlsTM:IDisposable
- {
- private Database db;
- private AutoCadTM tm;
- private Transaction ta;
- private BlockTable bt;
- private BlockTableRecord btr;
- private bool IsStarted=false;
- public TlsTM(bool Starting)
- {
- if(Starting)
- {
- db = HostApplicationServices.WorkingDatabase;
- tm = db.TransactionManager;
- ta = tm.StartTransaction();
- }
- IsStarted=Starting;
- }
- public Editor Editor
- {
- get
- {
- return Application.DocumentManager.MdiActiveDocument.Editor;
- }
- }
- public Database Database
- {
- get
- {
- return db;
- }
- }
- public AutoCadTM AutoCadTM
- {
- get
- {
- return tm;
- }
- }
- public Transaction Transaction
- {
- get
- {
- return ta;
- }
- }
- public BlockTable BlockTable
- {
- get
- {
- return bt;
- }
- }
- public BlockTableRecord BlockTableRecord
- {
- get
- {
- return btr;
- }
- }
- #region Add Entity
- public ObjectId Add(Entity entity)
- {
- ObjectId id = btr.AppendEntity(entity);
- tm.AddNewlyCreatedDBObject(entity, true);
- return id;
- }
- public ObjectIdCollection Add(DBObjectCollection objs)
- {
- ObjectIdCollection ids = new ObjectIdCollection();
- foreach(DBObject obj in objs)
- {
- ids.Add(this.Add((Entity)obj));
- }
- return ids;
- }
- public ObjectIdCollection Add(DBObject[] objs)
- {
- ObjectIdCollection ids = new ObjectIdCollection();
- foreach(DBObject obj in objs)
- {
- ids.Add(this.Add((Entity)obj));
- }
- return ids;
- }
- #endregion
- #region Remove Entity
- public bool Remove(ObjectId id)
- {
- DBObject obj;
- try
- {
- obj = tm.GetObject(id,OpenMode.ForWrite);
- obj.Erase(true);
- }
- catch
- {
- return false;
- }
- return true;
- }
- public bool Remove(ObjectIdCollection ids)
- {
- foreach(ObjectId id in ids)
- {
- try
- {
- DBObject obj;
- obj = tm.GetObject(id,OpenMode.ForWrite);
- obj.Erase(true);
- }
- catch
- {
- return false;
- }
- }
- return true;
- }
- public bool Remove(ObjectId[] ids)
- {
- foreach(ObjectId id in ids)
- {
- try
- {
- DBObject obj;
- obj = tm.GetObject(id,OpenMode.ForWrite);
- obj.Erase(true);
- }
- catch
- {
- return false;
- }
- }
- return true;
- }
- #endregion
- #region Trans
- public void OpenBlockTableRecord(string str)
- {
- bt = (BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false);
- btr = (BlockTableRecord)tm.GetObject(bt[str], OpenMode.ForWrite, false);
- }
- public Entity GetObject(ObjectId id,OpenMode mode)
- {
- return (Entity)tm.GetObject(id,mode,true);
- }
- void IDisposable.Dispose()
- {
- if(IsStarted)
- {
- ta.Commit();
- }
- ta.Dispose();
- }
- #endregion
- public void RegApp(string AppName)
- {
- RegAppTable tbl = (RegAppTable)tm.GetObject(db.RegAppTableId, OpenMode.ForWrite, false);
- if(!tbl.Has(AppName))
- {
- RegAppTableRecord app = new RegAppTableRecord();
- app.Name = AppName;
- tbl.Add(app);
- tm.AddNewlyCreatedDBObject(app, true);
- }
- }
- }
- }
|
|