马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
- using System;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using System.Collections.Generic;
- namespace Fsxm.Acad
- {
- //Jig动态变换图元
- public class Fsxm : DrawJig
- {
- Database db = HostApplicationServices.WorkingDatabase;
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- SelectionSet ss;
- BlockReference insert;
- Point3d minpt, oldpt;
- double width, higth;
- [CommandMethod("Trans", CommandFlags.UsePickSet)]
- public void Trans()
- {
- BlockTable bt;
- PromptSelectionOptions sop = new PromptSelectionOptions();
- sop.MessageForAdding = "选择你要变换的东东";
- sop.RejectObjectsOnLockedLayers = true;
- PromptSelectionResult srt = ed.GetSelection(sop);
- if (srt.Status != PromptStatus.OK) return;
- ss = srt.Value;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- //做块
- BlockTableRecord btr;
- btr = new BlockTableRecord();
- btr.Name = "*U";
- //btr.Origin = prt.Value;
- bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
- bt.Add(btr);
- trans.AddNewlyCreatedDBObject(btr, true);
- //块中图元
- foreach (SelectedObject sobj in ss)
- {
- Entity ent = (Entity)trans.GetObject(sobj.ObjectId, OpenMode.ForRead);
- Entity copy = (Entity)ent.Clone();
- btr.AppendEntity(copy);
- trans.AddNewlyCreatedDBObject(copy, true);
- ent.Highlight();//高亮要托动的东东
- }
- //算出块大小
- insert = new BlockReference(new Point3d(), btr.Id);
- Extents3d box = insert.GeometricExtents;
- Vector3d v = box.MaxPoint.GetVectorTo(box.MinPoint);
- width = v.X;
- higth = v.Y;
- //改下块原点
- btr.Origin = minpt = box.MinPoint;
- insert.Position = minpt;
- //递交
- trans.Commit();
- }
- //开始托动
- PromptResult var = ed.Drag(this);
- if (var.Status != PromptStatus.OK)
- {
- //去高亮
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- foreach (SelectedObject obj in ss)
- {
- Entity ent = (Entity)trans.GetObject(obj.ObjectId, OpenMode.ForWrite);
- ent.Unhighlight();
- }
- }
- return;
- }
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- //插入打散的块
- BlockTableRecord MSpace = (BlockTableRecord)trans.GetObject(bt["*Model_Space"], OpenMode.ForWrite);
- DBObjectCollection exs = new DBObjectCollection();
- insert.Explode(exs);
- foreach (DBObject obj in exs)
- {
- MSpace.AppendEntity((Entity)obj);
- trans.AddNewlyCreatedDBObject(obj, true);
- }
- //删除老线
- foreach (SelectedObject obj in ss)
- {
- Entity ent = (Entity)trans.GetObject(obj.ObjectId, OpenMode.ForWrite);
- ent.Erase();
- }
- //递交
- trans.Commit();
- }
- }
- protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
- {
- draw.Geometry.Draw(insert);
- return true;
- }
- protected override SamplerStatus Sampler(JigPrompts prompts)
- {
- Point3d cur = prompts.AcquirePoint("拾取变形目标点").Value;
- if (oldpt == cur)
- return SamplerStatus.NoChange;
- oldpt = cur;
- Vector3d v = cur.GetVectorTo(minpt);
- double scX = v.X / width;
- double scY = v.Y / higth;
- insert.ScaleFactors = new Scale3d(scX, scY, 1);
- return SamplerStatus.OK;
- }
- }
- }
|