private Curve2d StructurePosCurve(CivilDB.Structure str) { switch (str.BoundingShape) { case CivilDB.BoundingShapeType.Cylinder: return(new CircularArc2d(new Point2d(str.Location.X, str.Location.Y), str.InnerDiameterOrWidth / 2)); case CivilDB.BoundingShapeType.Box: //создать прямоугольник с учетом поворота и точки вставки Vector2d loc = new Vector2d(str.Location.X, str.Location.Y); Matrix2d rotation = Matrix2d.Rotation(str.Rotation, Point2d.Origin); Point2d p0 = (new Point2d(-str.InnerDiameterOrWidth / 2, -str.InnerLength / 2)).TransformBy(rotation) + loc; Point2d p1 = (new Point2d(-str.InnerDiameterOrWidth / 2, str.InnerLength / 2)).TransformBy(rotation) + loc; Point2d p2 = (new Point2d(str.InnerDiameterOrWidth / 2, str.InnerLength / 2)).TransformBy(rotation) + loc; Point2d p3 = (new Point2d(str.InnerDiameterOrWidth / 2, -str.InnerLength / 2)).TransformBy(rotation) + loc; LineSegment2d side0 = new LineSegment2d(p0, p1); LineSegment2d side1 = new LineSegment2d(p1, p2); LineSegment2d side2 = new LineSegment2d(p2, p3); LineSegment2d side3 = new LineSegment2d(p3, p0); return(new CompositeCurve2d(new Curve2d[] { side0, side1, side2, side3 })); default: return(null); } }
/* * Calculates transform matrix from layout coordinate system to view coordinate system. */ private static Matrix2d LayoutTransform(RectangleF bounds, LinearLayoutDirection layoutDirection, LinearLayoutDirection flowDirection) { switch (layoutDirection) { case LinearLayoutDirection.LeftRight: switch (flowDirection) { case LinearLayoutDirection.TopBottom: return(new Matrix2d()); case LinearLayoutDirection.BottomTop: return(Matrix2d.Translation(0, bounds.Height) * Matrix2d.Stretch(1, -1)); } break; case LinearLayoutDirection.RightLeft: switch (flowDirection) { case LinearLayoutDirection.TopBottom: return(Matrix2d.Translation(bounds.Width, 0) * Matrix2d.Stretch(-1, 1)); case LinearLayoutDirection.BottomTop: return(Matrix2d.Translation(bounds.Width, bounds.Height) * Matrix2d.Stretch(-1, -1)); } break; case LinearLayoutDirection.TopBottom: switch (flowDirection) { case LinearLayoutDirection.LeftRight: return(Matrix2d.Rotation(90) * Matrix2d.Stretch(1, -1)); case LinearLayoutDirection.RightLeft: return(Matrix2d.Translation(bounds.Width, 0) * Matrix2d.Rotation(90)); } break; case LinearLayoutDirection.BottomTop: switch (flowDirection) { case LinearLayoutDirection.LeftRight: return(Matrix2d.Translation(0, bounds.Height) * Matrix2d.Rotation(-90)); case LinearLayoutDirection.RightLeft: return(Matrix2d.Translation(bounds.Width, bounds.Height) * Matrix2d.Stretch(-1, 1) * Matrix2d.Rotation(-90)); } break; } var message = $"Layout direction {layoutDirection} is not compatible with flow direction {flowDirection}"; throw new ArgumentException(message); }
/// <summary> /// Поворот до ортогональности /// </summary> private void ToOrtho() { var pt1 = Cells.First(); var pt2 = Cells.Skip(1).First(); var vecSeg = pt2.PtCenter - pt1.PtCenter; double angleToOrtho; if (!vecSeg.Angle.IsOrthoAngle(out angleToOrtho)) { // Поворот дома на угол angleToOrtho до ортогонального положения вокруг центра контура var ptCenter = house.GetCenter(); var matRotate = Matrix2d.Rotation(angleToOrtho, ptCenter.Convert2d()); Trans(matRotate); } }
public void MyCommand() // This method can have any name { LayerStateManager LayerState = db.LayerStateManager; int viewnum = 0; ObjectId SaveTableID = InitialSave(); if (SaveTableID == ObjectId.Null) { ed.WriteMessage("\n遇到问题,无法创建存档..."); return; } int SaveNum = GetSave(SaveTableID); if (SaveNum != InitialNum) { PromptIntegerOptions InputOption = new PromptIntegerOptions("\n回车继续上次的编号,或者输入新的视图起始编号"); InputOption.AllowNone = true; InputOption.DefaultValue = SaveNum; PromptIntegerResult InputRes = ed.GetInteger(InputOption); if (InputRes.Status != PromptStatus.OK) { return; } viewnum = InputRes.Value; } else { PromptIntegerResult numres = ed.GetInteger("\n输入视图起始编号"); if (numres.Status != PromptStatus.OK) { return; } viewnum = numres.Value; } int counter = 0; while (true) { inputstart: PromptEntityOptions ops = new PromptEntityOptions("\n选择视图框"); ops.SetRejectMessage("\n只能选择封闭的矩形!"); ops.AddAllowedClass(typeof(Polyline), true); ops.AllowNone = false; ops.AllowObjectOnLockedLayer = true; PromptEntityResult res = ed.GetEntity(ops); if (res.Status != PromptStatus.OK) { if (counter != 0) { UpdateSave(SaveTableID, viewnum); } ed.WriteMessage("\n共创建{0}个视图。", counter); break; } using (Transaction trans = db.TransactionManager.StartTransaction()) { try { Polyline PL = trans.GetObject(res.ObjectId, OpenMode.ForRead) as Polyline; if (PL.NumberOfVertices != 4 && !PL.Closed) { ed.WriteMessage("\n选择的不是封闭的矩形!"); goto inputstart; } Point3d P1 = PL.GetPoint3dAt(0); Point3d P2 = PL.GetPoint3dAt(1); Point3d P3 = PL.GetPoint3dAt(2); Point3d P4 = PL.GetPoint3dAt(3); Vector3d V1 = P1.GetVectorTo(P2); Vector3d V2 = P2.GetVectorTo(P3); Vector3d V3 = P3.GetVectorTo(P4); Vector3d V4 = P4.GetVectorTo(P1); if (!(V1.IsPerpendicularTo(V2) && V2.IsPerpendicularTo(V3) && V3.IsPerpendicularTo(V4))) { ed.WriteMessage("\n选择的不是封闭的矩形!"); goto inputstart; } Point2d CT = new Point2d((P1.X + P3.X) / 2, (P1.Y + P3.Y) / 2); double H = 1; double W = 1; if (V1.Length > V4.Length) { H = V4.Length; W = V1.Length; } else { H = V1.Length; W = V4.Length; } Vector2d UserXaxix = new Vector2d(db.Ucsxdir.X, db.Ucsxdir.Y); Vector2d WXaxix = new Vector2d(1, 0); double TwistAngle = WXaxix.GetAngleTo(UserXaxix); if (db.Ucsxdir.Y > 0) { TwistAngle = Math.PI * 2 - WXaxix.GetAngleTo(UserXaxix); } //Matrix2d WCS2DCS = Matrix2d.Rotation(new Vector2d(1, 0).GetAngleTo(UserXaxix),new Point2d(0,0)); Matrix2d WCS2DCS = Matrix2d.Rotation(TwistAngle, new Point2d(0, 0)); Point3d UcsCenter = db.Ucsorg; //Point2d DcsCenter = new Point2d(UcsCenter.X, UcsCenter.Y).TransformBy(TransWCSToDCS); Point2d DcsCenter = CT.TransformBy(WCS2DCS); /* * ed.WriteMessage("\n计算旋转角:{0}", WXaxix.GetAngleTo(UserXaxix)); * ed.WriteMessage("\nTwistAngle:{0}", TwistAngle); * ed.WriteMessage("\nWCS的中点:{0}", CT.ToString()); * ed.WriteMessage("\nUCS的中点:{0}", new Point3d(CT.X,CT.Y,0).TransformBy(ed.CurrentUserCoordinateSystem).ToString()); * ed.WriteMessage("\nDCS的中点:{0}", DcsCenter.ToString()); */ SymbolTable VT = trans.GetObject(db.ViewTableId, OpenMode.ForWrite) as SymbolTable; if (VT.Has(viewnum.ToString())) { foreach (ObjectId viewid in VT) { ViewTableRecord VR = trans.GetObject(viewid, OpenMode.ForWrite) as ViewTableRecord; if (VR.Name == viewnum.ToString()) { VR.Erase(); break; } } } ViewTableRecord NewVr = new ViewTableRecord(); NewVr.Name = viewnum.ToString(); NewVr.CenterPoint = DcsCenter; NewVr.Height = H; NewVr.Width = W; NewVr.ViewTwist = TwistAngle; NewVr.SetUcs(db.Ucsorg, db.Ucsxdir, db.Ucsydir); VT.Add(NewVr); trans.AddNewlyCreatedDBObject(NewVr, true); //添加图层快照属性要在把view添加到数据库里后再操作,要不会报错eNoDataBase... string LayerStateName = string.Format("ACAD_VIEWS_{0}", NewVr.Name); //已有同名那就删掉 if (LayerState.HasLayerState(LayerStateName)) { LayerState.DeleteLayerState(LayerStateName); } LayerState.SaveLayerState(LayerStateName, LayerStateMasks.None, new ObjectId()); NewVr.LayerState = LayerStateName; trans.Commit(); ed.WriteMessage("\n成功创建编号为{0}的视图。", viewnum); viewnum++; counter++; } catch (Autodesk.AutoCAD.Runtime.Exception EX) { ed.WriteMessage("\n出错了!{0}", EX.ToString()); } finally { trans.Dispose(); } } } }