public void GetJingXiang() { var mirrText = Application.GetSystemVariable("MIRRTEXT"); if (mirrText != null && mirrText.ToString() == "1") { Application.SetSystemVariable("MIRRTEXT", 0); } DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); //eLockViolation BlockReference br = GetBlockCondition("请选择要镜像的块") as BlockReference; if (br == null) { return; } AcadApplication app = Application.AcadApplication as AcadApplication; DBObjectCollection coll = new DBObjectCollection(); br.Explode(coll); Circle c = new Circle(br.Position, Vector3d.ZAxis, 0.010324); var listEnt = coll.Cast <Entity>().ToList(); listEnt.Add(c); var oIdColl = listEnt.ToSpace(); using (var trans = Db.TransactionManager.StartTransaction()) { var blkRef = trans.GetObject(br.ObjectId, OpenMode.ForWrite) as BlockReference; blkRef.Visible = false; blkRef.DowngradeOpen(); trans.Commit(); } app.ActiveDocument.SendCommand("mirror "); using (var trans = Db.TransactionManager.StartTransaction()) { var blkRef = trans.GetObject(br.ObjectId, OpenMode.ForWrite) as BlockReference; blkRef.Visible = true; blkRef.DowngradeOpen(); for (int i = 0; oIdColl != null && i < oIdColl.Count; i++) { var ent = trans.GetObject(oIdColl[i], OpenMode.ForWrite) as Entity; if (ent != null) { ent.Erase(true); } } trans.Commit(); } var selRes = Ed.GetSelection(); if (selRes.Status != PromptStatus.OK) { return; } var list = selRes.Value.GetObjectIds()?.ToList(); ObjectId recId = ObjectId.Null; Point3d ptPosC = Point3d.Origin; using (var trans = Db.TransactionManager.StartTransaction()) { var blkTbl = trans.GetObject(Db.BlockTableId, OpenMode.ForWrite) as BlockTable; BlockTableRecord blkRec = new BlockTableRecord(); blkRec.Units = br.BlockUnit; string blkName = br.Name + "_" + DateTime.Now.ToString("yyyyMMddHHmmssffff"); blkRec.Name = blkName; for (int i = 0; list != null && i < list.Count; i++) { var ent = trans.GetObject(list[i], OpenMode.ForWrite); if (ent != null) { bool flag = true; if (ent is Circle) { var cir = ent as Circle; if (cir.Radius == 0.010324) { ptPosC = cir.Center; cir.Erase(true); flag = false; } } if (flag) { Entity entCopy = ent.Clone() as Entity; ent.Erase(true); blkRec.AppendEntity(entCopy); } } } blkRec.Origin = ptPosC; recId = blkTbl.Add(blkRec); trans.AddNewlyCreatedDBObject(blkRec, true); trans.Commit(); } BlockReference brEnt = new BlockReference(ptPosC, recId); // var vec = br.Position - ptPosC; //brEnt.TransformBy(Matrix3d.Displacement(vec * 2.5)); brEnt.ToSpace(); Application.ShowAlertDialog("OK"); Application.SetSystemVariable("MIRRTEXT", mirrText); m_DocumentLock.Dispose(); }
public void GetJingXiangXY() { DocumentLock m_DocumentLock = null; object mirrText = ""; try { m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); mirrText = Application.GetSystemVariable("MIRRTEXT"); if (mirrText != null && mirrText.ToString() == "1") { Application.SetSystemVariable("MIRRTEXT", 0); } var br = GetBlockCondition("请选择要镜像的块") as BlockReference; string name = br.Name; if (br == null) { return; } IdMapping idmap = new IdMapping(); string xYJingXiang = ""; PromptKeywordOptions pkOpts = new PromptKeywordOptions("请选择镜像方向[X/Y]", "X Y"); var propRes = Ed.GetKeywords(pkOpts); if (propRes.Status != PromptStatus.OK) { return; } xYJingXiang = propRes.StringResult; DBObjectCollection coll = new DBObjectCollection(); br.Explode(coll); var listEnt = coll.Cast <Entity>().ToList(); Point3d ptPos = br.Position; Point3d ptMax = br.Bounds.Value.MaxPoint; Point3d ptMin = br.Bounds.Value.MinPoint; double maxY = Math.Abs(ptMax.Y - ptMin.Y); double maxX = Math.Abs(ptMax.X - ptMin.X); if (xYJingXiang == "Y") { Point3d ptEnd = ptPos + Vector3d.YAxis * 100; Line lineY = new Line(ptPos, ptEnd); lineY.TransformBy(Matrix3d.Displacement(Vector3d.XAxis * maxX)); MyMirror(listEnt, lineY, "Y"); } else if (xYJingXiang == "X") { Point3d ptEnd = ptPos + Vector3d.XAxis * 100; Line lineX = new Line(ptPos, ptEnd); lineX.TransformBy(Matrix3d.Displacement(Vector3d.YAxis * maxY)); MyMirror(listEnt, lineX, "X"); } var brNew = new BlockReference(br.Position, br.BlockTableRecord); brNew.ToSpace(); Application.ShowAlertDialog("OK"); } catch (System.Exception e) { Ed.WriteMessage(e.ToString()); } finally { Application.SetSystemVariable("MIRRTEXT", mirrText); m_DocumentLock.Dispose(); } }
//[CommandMethod("ECDHeBing")] public void ActiveDocument_EndCommand() { DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); using (var trans = Db.TransactionManager.StartTransaction()) { var blkRef = trans.GetObject(br.ObjectId, OpenMode.ForWrite) as BlockReference; blkRef.Visible = true; blkRef.DowngradeOpen(); for (int i = 0; oIdColl != null && i < oIdColl.Count; i++) { var ent = trans.GetObject(oIdColl[i], OpenMode.ForWrite) as Entity; if (ent != null) { ent.Erase(true); } } trans.Commit(); } var selRes = Ed.GetSelection(); if (selRes.Status != PromptStatus.OK) { return; } var list = selRes.Value.GetObjectIds()?.ToList(); ObjectId recId = ObjectId.Null; Point3d ptPosC = Point3d.Origin; using (var trans = Db.TransactionManager.StartTransaction()) { var blkTbl = trans.GetObject(Db.BlockTableId, OpenMode.ForWrite) as BlockTable; BlockTableRecord blkRec = new BlockTableRecord(); blkRec.Units = br.BlockUnit; string blkName = br.Name + "_" + DateTime.Now.ToString("yyyyMMddHHmmssffff"); blkRec.Name = blkName; for (int i = 0; list != null && i < list.Count; i++) { var ent = trans.GetObject(list[i], OpenMode.ForWrite); if (ent != null) { bool flag = true; if (ent is Circle) { var cir = ent as Circle; if (cir.Radius == 0.010324) { ptPosC = cir.Center; cir.Erase(true); flag = false; } } if (flag) { Entity entCopy = ent.Clone() as Entity; ent.Erase(true); blkRec.AppendEntity(entCopy); } } } blkRec.Origin = ptPosC; recId = blkTbl.Add(blkRec); trans.AddNewlyCreatedDBObject(blkRec, true); trans.Commit(); } BlockReference brEnt = new BlockReference(ptPosC, recId); brEnt.ToSpace(); Application.ShowAlertDialog("OK"); Application.SetSystemVariable("MIRRTEXT", mirrText); m_DocumentLock.Dispose(); }
public void GetJingXiangXY() { DocumentLock m_DocumentLock = null; blkRef = null; list.Clear(); listOId.Clear(); XY = ""; try { m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); var br = GetBlockCondition("请选择要镜像的块") as BlockReference; string name = br.Name; if (br == null) { return; } blkRef = br; Circle circle = new Circle(blkRef.Position, Vector3d.ZAxis, 0.001); listOId.Add(circle.ToSpace()); Circle mirrorCircle = null; string xYJingXiang = ""; PromptKeywordOptions pkOpts = new PromptKeywordOptions("请选择镜像方向[X/Y]", "X Y"); var propRes = Ed.GetKeywords(pkOpts); if (propRes.Status != PromptStatus.OK) { return; } xYJingXiang = propRes.StringResult; DBObjectCollection coll = new DBObjectCollection(); br.Explode(coll); var listEnt = coll.Cast <Entity>().ToList(); Point3d ptPos = br.Position; Point3d ptMax = br.Bounds.Value.MaxPoint; Point3d ptMin = br.Bounds.Value.MinPoint; double maxY = Math.Abs(ptMax.Y - ptMin.Y); double maxX = Math.Abs(ptMax.X - ptMin.X); using (Transaction tr = blkRef.Database.TransactionManager.StartTransaction()) { if (xYJingXiang == "Y") { Point3d ptEnd = ptPos + Vector3d.YAxis * 100; Line lineY = new Line(ptPos, ptEnd); lineY.TransformBy(Matrix3d.Displacement(Vector3d.XAxis * maxX)); XY = "Y"; mirrorCircle = circle.GetTransformedCopy(Matrix3d.Mirroring(new Line3d(lineY.StartPoint, lineY.EndPoint))) as Circle; MyMirror(listEnt, lineY, "Y"); } else if (xYJingXiang == "X") { Point3d ptEnd = ptPos + Vector3d.XAxis * 100; Line lineX = new Line(ptPos, ptEnd); lineX.TransformBy(Matrix3d.Displacement(Vector3d.YAxis * maxY)); XY = "X"; mirrorCircle = circle.GetTransformedCopy(Matrix3d.Mirroring(new Line3d(lineX.StartPoint, lineX.EndPoint))) as Circle; MyMirror(listEnt, lineX, "X"); } tr.Commit(); } ObjectId breNewId = ObjectId.Null; using (var trans = blkRef.Database.TransactionManager.StartTransaction()) { var blkTbl = trans.GetObject(blkRef.Database.BlockTableId, OpenMode.ForWrite) as BlockTable; BlockTableRecord blkRec = new BlockTableRecord(); blkRec.Units = br.BlockUnit; string blkName = br.Name + "_" + DateTime.Now.ToString("yyyyMMddHHmmssffff"); blkRec.Name = blkName; blkRec.Origin = mirrorCircle.Center; for (int i = 0; list != null && i < list.Count; i++) { var ent = list[i]; if (ent != null) { //Entity entCopy = ent.Clone() as Entity; // ent.Erase(true); blkRec.AppendEntity(ent); //ent.ToSpace(); } } breNewId = blkTbl.Add(blkRec); trans.AddNewlyCreatedDBObject(blkRec, true); foreach (var oId in listOId) { var ent = trans.GetObject(oId, OpenMode.ForWrite) as Entity; ent.Erase(true); } listOId.Clear(); trans.Commit(); } list.ForEach(ent => ent.Dispose()); var brOld = new BlockReference(br.Position, br.BlockTableRecord); var brNew = new BlockReference(mirrorCircle.Center, breNewId); brNew.ToSpace(); brOld.ToSpace(); } catch (System.Exception e) { Ed.WriteMessage(e.ToString()); } finally { m_DocumentLock.Dispose(); } }