c#+CAD动态移动效果

  1  public class MoveRotateScaleJig : DrawJig
  2     {
  3         public static List<Entity> entities = new List<Entity>();
  4         private int step = 1;
  5         private int totalStepNum = 3;
  6 
  7         public Point3d moveStartPnt;
  8         public static Point3d moveEndPnt;
  9         public Double rotateAngle;
 10         public Double scaleFactor;
 11 
 12         public MoveRotateScaleJig(Point3d basePnt)
 13         {
 14             moveStartPnt = basePnt;
 15             moveEndPnt = moveStartPnt;
 16             rotateAngle = 0;
 17             scaleFactor = 1;
 18         }
 19 
 20         public Matrix3d Transformation
 21         {
 22             get
 23             {
 24                 return Matrix3d.Scaling(scaleFactor, moveEndPnt).
 25                  PostMultiplyBy(Matrix3d.Rotation(rotateAngle, Vector3d.ZAxis, moveEndPnt)).
 26                  PostMultiplyBy(Matrix3d.Displacement(moveStartPnt.GetVectorTo(moveEndPnt)));
 27             }
 28         }
 29 
 30         public void AddEntity(Entity ent)
 31         {
 32             entities.Add(ent);
 33         }
 34 
 35         public void TransformEntities()
 36         {
 37             Matrix3d mat = Transformation;
 38             foreach (Entity ent in entities)
 39             {
 40                 ent.TransformBy(mat);
 41             }
 42         }
 43 
 44         protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
 45         {
 46             Matrix3d mat = Transformation;
 47 
 48             WorldGeometry geo = draw.Geometry;
 49             if (geo != null)
 50             {
 51                 geo.PushModelTransform(mat);
 52 
 53                 foreach (Entity ent in entities)
 54                 {
 55                     geo.Draw(ent);
 56                 }
 57 
 58                 geo.PopModelTransform();
 59             }
 60 
 61             return true;
 62         }
 63 
 64         protected override SamplerStatus Sampler(JigPrompts prompts)
 65         {
 66             switch (step)
 67             {
 68                 case 1:
 69                     JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\n目标点:");
 70                     prOptions1.UserInputControls = UserInputControls.GovernedByOrthoMode
 71                         | UserInputControls.GovernedByUCSDetect;
 72                     PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
 73                     if (prResult1.Status != PromptStatus.OK)
 74                         return SamplerStatus.Cancel;
 75 
 76                     if (prResult1.Value.Equals(moveEndPnt))
 77                     {
 78                         return SamplerStatus.NoChange;
 79                     }
 80                     else
 81                     {
 82                         moveEndPnt = prResult1.Value;
 83                         return SamplerStatus.OK;
 84                     }
 85 
 86                 case 2:
 87                     JigPromptAngleOptions prOptions2 = new JigPromptAngleOptions("\nRotate:");
 88                     prOptions2.UseBasePoint = true;
 89                     prOptions2.BasePoint = moveEndPnt;
 90                     prOptions2.UserInputControls = UserInputControls.GovernedByOrthoMode
 91                         | UserInputControls.GovernedByUCSDetect;
 92                     PromptDoubleResult prResult2 = prompts.AcquireAngle(prOptions2);
 93                     if (prResult2.Status != PromptStatus.OK)
 94                         return SamplerStatus.Cancel;
 95 
 96                     if (prResult2.Value.Equals(rotateAngle))
 97                     {
 98                         return SamplerStatus.NoChange;
 99                     }
100                     else
101                     {
102                         rotateAngle = prResult2.Value;
103                         return SamplerStatus.OK;
104                     }
105 
106                 case 3:
107                     JigPromptDistanceOptions prOptions3 = new JigPromptDistanceOptions("\nScale:");
108                     prOptions3.UseBasePoint = true;
109                     prOptions3.BasePoint = moveEndPnt;
110                     prOptions3.UserInputControls = UserInputControls.GovernedByOrthoMode
111                         | UserInputControls.GovernedByUCSDetect;
112                     PromptDoubleResult prResult3 = prompts.AcquireDistance(prOptions3);
113                     if (prResult3.Status != PromptStatus.OK)
114                         return SamplerStatus.Cancel;
115 
116                     if (prResult3.Value.Equals(scaleFactor))
117                     {
118                         return SamplerStatus.NoChange;
119                     }
120                     else
121                     {
122                         scaleFactor = prResult3.Value;
123                         return SamplerStatus.OK;
124                     }
125 
126                 default:
127                     break;
128             }
129 
130             return SamplerStatus.OK;
131         }
132 
133         public static bool Jig()
134         {
135             try
136             {
137                 Document doc = Application.DocumentManager.MdiActiveDocument;
138                 Database db = doc.Database;
139 
140                 // 选择对象
141                 PromptSelectionResult selRes = doc.Editor.GetSelection();
142                 if (selRes.Status != PromptStatus.OK)
143                     return false;
144 
145                 // 指定起点
146                 PromptPointResult ppr = doc.Editor.GetPoint("\nStart point:");
147                 if (ppr.Status != PromptStatus.OK)
148                     return false;
149                 Point3d basePnt = ppr.Value;
150                 basePnt = basePnt.TransformBy(doc.Editor.CurrentUserCoordinateSystem);
151 
152                 // Draw Jig
153                 MoveRotateScaleJig jig = new MoveRotateScaleJig(basePnt);
154                 using (Transaction tr = db.TransactionManager.StartTransaction())
155                 {
156                     foreach (ObjectId id in selRes.Value.GetObjectIds())
157                     {
158                         Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
159                         jig.AddEntity(ent);
160                     }
161 
162                     // Draw Jig 交互
163                     PromptResult pr;
164                     do
165                     {
166                         pr = doc.Editor.Drag(jig);
167                         if (pr.Status == PromptStatus.Keyword)
168                         {
169                             // Keyword handling code
170                         }
171                         else
172                         {
173                             jig.step++;
174                         }
175                     }
176                     while (pr.Status == PromptStatus.OK
177                      && jig.step <= jig.totalStepNum);
178 
179                     // 结果
180                     if (pr.Status == PromptStatus.OK &&
181                         jig.step == jig.totalStepNum + 1)
182                     {
183                         jig.TransformEntities();
184                     }
185                     else
186                     {
187                         return false;
188                     }
189 
190                 
191                     tr.Commit();
192                     return true;
193                 }
194             }
195             catch
196             {
197                 return false;
198             }
199         }
200 
201         public static bool JigMove()
202         {
203             try
204             {
205                 Document doc = Application.DocumentManager.MdiActiveDocument;
206                  Database db = doc.Database;
207 
208                 //// 选择对象
209                 //PromptSelectionResult selRes = doc.Editor.GetSelection();
210                 //if (selRes.Status != PromptStatus.OK)
211                 //    return false;
212 
213                 // 指定起点
214                 PromptPointResult ppr = doc.Editor.GetPoint("\n指定起点:");
215                 if (ppr.Status != PromptStatus.OK)
216                     return false;
217                 Point3d basePnt = ppr.Value;
218                 basePnt = basePnt.TransformBy(doc.Editor.CurrentUserCoordinateSystem);
219 
220                 // Draw Jig
221                 MoveRotateScaleJig jig = new MoveRotateScaleJig(basePnt);
222                 using (Transaction tr = db.TransactionManager.StartTransaction())
223                 {
224                     //foreach (ObjectId id in selRes.Value.GetObjectIds())
225                     //{
226                     //    Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
227                     //}
228 
229                     // Draw Jig 交互
230                     PromptResult pr;
231                     do
232                     {
233                         pr = doc.Editor.Drag(jig);
234                         if (pr.Status == PromptStatus.Keyword)
235                         {
236                             // Keyword handling code
237                         }
238                         else
239                         {
240                             for (int i = 0; i < entities.Count;i++ )
241                             {
242                                 Entity ent = tr.GetObject(entities[i].ObjectId, OpenMode.ForWrite) as Entity;
243                                 AcEdPublicDll.CAcadEntity.Move(ent, basePnt, moveEndPnt);
244                                 ent.Dispose();
245                             }
246                         }
247                     }
248                     while (pr.Status != PromptStatus.OK);
249                     tr.Commit();
250                     return true;
251                 }
252             }
253             catch
254             {
255                 return false;
256             }
257         }
258     }

调用:MoveRotateScaleJig.Jig()

猜你喜欢

转载自www.cnblogs.com/happyqiang/p/10418162.html