//-------------------------------------------------------------------------------------------------- public static bool GetCenteredPlaneFromFace(TopoDS_Face face, out Pln plane) { var brepAdaptor = new BRepAdaptor_Surface(face, true); if (brepAdaptor.GetGeomType() != GeomAbs_SurfaceType.GeomAbs_Plane) { Messages.Error("Selected face is not a plane type surface."); plane = Pln.XOY; return(false); } double centerU = brepAdaptor.FirstUParameter() + (brepAdaptor.LastUParameter() - brepAdaptor.FirstUParameter()) / 2; double centerV = brepAdaptor.FirstVParameter() + (brepAdaptor.LastVParameter() - brepAdaptor.FirstVParameter()) / 2; var centerPnt = brepAdaptor.Value(centerU, centerV); var pos = brepAdaptor.Plane().Position; var dir = brepAdaptor.Plane().Position.Direction; if (face.Orientation() == TopAbs_Orientation.TopAbs_REVERSED) { dir.Reverse(); } if (pos.Direction.DotCross(pos.XDirection, pos.YDirection) < 0) { dir.Reverse(); } plane = new Pln(centerPnt, dir); return(true); }
//-------------------------------------------------------------------------------------------------- public static Pnt2d Parameters(this Pln pln, Pnt pnt) { double u = 0, v = 0; ElSLib.Parameters(pln, pnt, ref u, ref v); return(new Pnt2d(u, v)); }
//-------------------------------------------------------------------------------------------------- void _FinishPivotPoint(ToolAction toolAction) { if (!(toolAction is PointAction pointAction)) { return; } _Plane = WorkspaceController.Workspace.WorkingPlane; _PointPlane1 = pointAction.PointOnPlane; _PivotPoint = pointAction.Point; pointAction.Stop(); pointAction = new PointAction(this); if (!WorkspaceController.StartToolAction(pointAction)) { return; } pointAction.Previewed += _PreviewRadius; pointAction.Finished += _FinishRadius; _CurrentPhase = Phase.Radius; StatusText = "Select radius."; _ValueHudElement = WorkspaceController.HudManager?.CreateElement <ValueHudElement>(this); if (_ValueHudElement != null) { _ValueHudElement.Label = "Radius:"; _ValueHudElement.Units = ValueUnits.Length; _ValueHudElement.ValueEntered += _ValueEntered; } WorkspaceController.HudManager?.SetCursor(Cursors.SetRadius); }
//-------------------------------------------------------------------------------------------------- public void EnableClipPlane(bool enable) { if (enable) { if (_ClipPlane != null) { return; } var sketchPlane = Sketch.Plane; var reversedPlane = new Pln(new Ax3(sketchPlane.Location, sketchPlane.Axis.Direction.Reversed())); reversedPlane.Translate(reversedPlane.Axis.Direction.Reversed().ToVec().Scaled(0.0001)); _ClipPlane = new ClipPlane(reversedPlane); _ClipPlane.AddViewport(WorkspaceController.ActiveViewport); WorkspaceController.Invalidate(); RaisePropertyChanged(nameof(ClipPlaneEnabled)); } else { if (_ClipPlane == null) { return; } _ClipPlane.Remove(); _ClipPlane = null; WorkspaceController.Invalidate(); RaisePropertyChanged(nameof(ClipPlaneEnabled)); } }
//-------------------------------------------------------------------------------------------------- public bool ScreenToPoint(Pln plane, int screenX, int screenY, out Pnt resultPnt) { try { double xv = 0, yv = 0, zv = 0; double vx = 0, vy = 0, vz = 0; V3dView.Convert(screenX, screenY, ref xv, ref yv, ref zv); V3dView.Proj(ref vx, ref vy, ref vz); gp_Lin line = new gp_Lin(new Pnt(xv, yv, zv), new Dir(vx, vy, vz)); IntAna_IntConicQuad intersection = new IntAna_IntConicQuad(line, plane, Precision.Angular(), 0, 0); if (intersection.IsDone() && !intersection.IsParallel() && intersection.NbPoints() > 0) { resultPnt = intersection.Point(1); return(true); } } catch (Exception) { Debug.Assert(false); } resultPnt = new Pnt(); return(false); }
public static TopoDS_Edge MakeEdge(Geom2d_Curve curve2d, Pln plane) { var curve = GeomAPI.To3d(curve2d, plane); var edge = new BRepBuilderAPI_MakeEdge(curve).Edge(); return(edge); }
//-------------------------------------------------------------------------------------------------- double?_ProcessMouseInputForAxis(MouseEventData data) { var planeDir = WorkspaceController.ActiveViewport.GetRightDirection(); if (planeDir.IsParallel(_MoveAxis.Direction, 0.1)) { planeDir = WorkspaceController.ActiveViewport.GetUpDirection(); } planeDir.Cross(_MoveAxis.Direction); //Console.WriteLine("PlaneDir: {0:0.00} | {1:0.00} | {2:0.00}", planeDir.X(), planeDir.Y(), planeDir.Z()); var plane = new Pln(new Ax3(_MoveAxis.Location, planeDir, _MoveAxis.Direction)); Pnt convertedPoint; if (WorkspaceController.ActiveViewport.ScreenToPoint(plane, Convert.ToInt32(data.ScreenPoint.X), Convert.ToInt32(data.ScreenPoint.Y), out convertedPoint)) { var extrema = new Extrema_ExtPC(convertedPoint, new GeomAdaptor_Curve(new Geom_Line(_MoveAxis)), 1.0e-10); if (extrema.IsDone() && extrema.NbExt() >= 1) { var value = extrema.Point(1).Parameter(); return(value); } } return(null); }
//-------------------------------------------------------------------------------------------------- void _FinishPivotPoint(ToolAction toolAction) { if (!(toolAction is PointAction pointAction)) { return; } _ClearPreviews(); _Plane = WorkspaceController.Workspace.WorkingPlane; _PointPlane1 = pointAction.PointOnPlane; pointAction.Stop(); pointAction = new PointAction(this); pointAction.Previewed += _PreviewBaseRect; pointAction.Finished += _FinishBaseRect; if (!WorkspaceController.StartToolAction(pointAction)) { return; } _CurrentPhase = Phase.BaseRect; StatusText = "Select opposite corner point."; _MultiValueHudElement = WorkspaceController.HudManager?.CreateElement <MultiValueHudElement>(this); if (_MultiValueHudElement != null) { _MultiValueHudElement.Label1 = "Length:"; _MultiValueHudElement.Units1 = ValueUnits.Length; _MultiValueHudElement.Label2 = "Width:"; _MultiValueHudElement.Units2 = ValueUnits.Length; _MultiValueHudElement.MultiValueEntered += _MultiValueEntered; } }
//-------------------------------------------------------------------------------------------------- /* * The start face is a face which is parallel to the base plane of the shape (X/Y plane). * Take the face with the shortest distance to the plane. This should work for 99%. */ public static TopoDS_Face FindFaceNearestToPlane(TopoDS_Shape sourceShape, Pln plane) { if (sourceShape == null) { return(null); } var faces = sourceShape.Faces(); TopoDS_Face bestMatch = null; double bestDistance = Double.MaxValue; foreach (var face in faces) { if (!GetPlaneFromFace(face, out var facePlane)) { continue; } if (!facePlane.Axis.IsParallel(plane.Axis, 0.01)) { continue; } var dist = facePlane.SquareDistance(plane); if (dist >= bestDistance) { continue; } bestMatch = face; bestDistance = dist; } return(bestMatch); }
//-------------------------------------------------------------------------------------------------- public ClipPlane(Pln plane) { _OcClipPlane = new Graphic3d_ClipPlane(plane); _UpdateAspects(); _OcClipPlane.SetOn(true); }
//-------------------------------------------------------------------------------------------------- public void Set(Pnt2d p, Pln plane) { Pnt pnt = new Pnt(); ElSLib.D0(p.X, p.Y, plane, ref pnt); Set(pnt); }
public void Rotate() { Pln p1 = Pln.XOY; p1.Rotate(Ax1.OZ, Math.PI / 2); Assert.That(Pnt.Origin.IsEqual(p1.Location, 0.0000001)); Assert.That(new Dir(0, 1, 0).IsEqual(p1.XAxis.Direction, 0.0000001)); }
//-------------------------------------------------------------------------------------------------- #endregion #region Pln public static Quaternion Rotation(this Pln plane) { var mat = new Mat( plane.XAxis.Direction.Coord, plane.YAxis.Direction.Coord, plane.Axis.Direction.Coord); return(new Quaternion(mat)); }
//-------------------------------------------------------------------------------------------------- public void Set(Pnt2d p1, Pnt2d p2, Pln plane) { Pnt pnt1 = new Pnt(); Pnt pnt2 = new Pnt(); ElSLib.D0(p1.X, p1.Y, plane, ref pnt1); ElSLib.D0(p2.X, p2.Y, plane, ref pnt2); Set(pnt1, pnt2); }
public void Origins() { Pln p1 = Pln.XOY; double a = 0, b = 0, c = 0, d = 0; p1.Coefficients(ref a, ref b, ref c, ref d); Assert.AreEqual(0, a, 0.000001); Assert.AreEqual(0, b, 0.000001); Assert.AreEqual(1, c, 0.000001); Assert.AreEqual(0, d, 0.000001); }
public void ValueType_Pln() { Pln p1 = Pln.XOY; double a = 0, b = 0, c = 0, d = 0; p1.Coefficients(ref a, ref b, ref c, ref d); Assert.AreEqual("0,0,1,0", $"{a},{b},{c},{d}"); p1.Rotate(Ax1.OZ, Math.PI / 2); Assert.AreEqual("(0,0,0),(1.11022302462516E-16,1,0)", p1.XAxis.ToString()); }
//-------------------------------------------------------------------------------------------------- #endregion #region Profiles TopoDS_Shape _CreateProfileFromSketch(Pln plane) { var baseFacesShape = GetOperand2DFaces(1, plane); if (baseFacesShape == null) { Messages.Error("Cannot create profile from 2D operand."); return(null); } return(baseFacesShape); }
protected void TEST() { SBMObjectList <Project> PrjLst = null; AppObjectList <CalendarWeek> CalWkLst; Project Prj; Category Cat; LineItem LnItem; CostCenter CosCen; CalendarWeek CalWk; SBMObjectList <Planning> PlnLst = null; Planning Pln; PlanningRate PlnRat; object objVal; Application.GetCategories(); PrjLst = Application.GetProjects(); for (int nPrjIdx = 0; nPrjIdx < PrjLst.Count; nPrjIdx++) { Prj = PrjLst[nPrjIdx]; Cat = Prj.GetCategory(); LnItem = Prj.GetLineItem(); CosCen = Prj.GetCostCenter(); if (Prj.GetNumber() == 500097) { PlnLst = Prj.GetPlannings(); for (int nPlnIdx = 0; nPlnIdx < PlnLst.Count; nPlnIdx++) { Pln = PlnLst[nPlnIdx]; for (int nRatIdx = 0; nRatIdx < Pln.GetPlanningRates().Count; nRatIdx++) { PlnRat = Pln.GetPlanningRates().GetAt(nRatIdx); CalWk = PlnRat.GetCalendarWeek(); } } } } CalWkLst = Application.GetCalendar().GetCalendarWeeks(); for (int nIdx = 0; nIdx < CalWkLst.Count; nIdx++) { CalWk = CalWkLst[nIdx]; objVal = CalWk.FirstDay; objVal = CalWk.LastDay; objVal = CalWk.OrderIndex; } }
//-------------------------------------------------------------------------------------------------- IEnumerable <Geom_Curve> _CreateCircleProfileCurves(Pln plane, double sx, double sy) { if (_Flags.HasFlag(PipeFlags.SymmetricProfile)) { yield return(new Geom_Circle(plane.Position.ToAx2(), sx)); } else { yield return(sx > sy ? new Geom_Ellipse(plane.Position.Rotated(plane.Axis, _Rotation.ToRad()).ToAx2(), sx, sy) : new Geom_Ellipse(plane.Position.Rotated(plane.Axis, _Rotation.ToRad() + Maths.HalfPI).ToAx2(), sy, sx)); } }
public static bool GetPlaneFromFace(TopoDS_Face face, out Pln plane) { var surfaceAdaptor = new BRepAdaptor_Surface(face); if (surfaceAdaptor.GetGeomType() != GeomAbs_SurfaceType.GeomAbs_Plane) { plane = new Pln(); return(false); } plane = surfaceAdaptor.Plane(); return(true); }
//-------------------------------------------------------------------------------------------------- public void Set(Pnt2d p1, Pnt2d p2, Pln plane) { if (p1.IsEqual(p2, Double.Epsilon)) { return; } Pnt pnt1 = new Pnt(); Pnt pnt2 = new Pnt(); ElSLib.D0(p1.X, p1.Y, plane, ref pnt1); ElSLib.D0(p2.X, p2.Y, plane, ref pnt2); Set(pnt1, pnt2); }
//-------------------------------------------------------------------------------------------------- bool _CreateProjectedPointList(List <Pnt> vertexList) { _PointList = new List <Pnt2d>(_IndexList.Count); // Build plane var pnts = new TColgp_HArray1OfPnt(1, _IndexList.Count); for (int i = 0; i < _IndexList.Count; i++) { pnts.SetValue(i + 1, vertexList[_IndexList[i]]); } var buildPlane = new GeomPlate_BuildAveragePlane(pnts, pnts.Length(), 0, 1, 1); if (!buildPlane.IsPlane()) { return(false); } Pln plane = buildPlane.Plane().Pln(); //plane.Location = Pnt.Origin; // Project points on plane double u = 0, v = 0; foreach (var i in _IndexList) { ElSLib.Parameters(plane, vertexList[i], ref u, ref v); _PointList.Add(new Pnt2d(u, v)); } // Get winding order of polygon double area = 0; for (int i = 0; i < _PointList.Count - 1; i++) { area += _PointList[i].X * _PointList[i + 1].Y - _PointList[i + 1].X * _PointList[i].Y; } if (area < 0) { _PointList.Reverse(); _IndexList.Reverse(); } return(true); }
//-------------------------------------------------------------------------------------------------- public override bool OnMouseDown(MouseEventData data) { _RotateMode = RotateMode.None; if (data.DetectedEntities.Count == 0 && _Gizmo != null) { if (_Gizmo.IsPartDetected(AIS_RotationGizmo.Part.Part_XAxis)) { _RotateMode = RotateMode.AxisX; RotationAxis = new Ax1(_CoordinateSystem.Location, _CoordinateSystem.XDirection); _RotationPlane = new Pln(new Ax3(_CoordinateSystem.Location, _CoordinateSystem.XDirection, _CoordinateSystem.Direction)); _Gizmo.ForcePartHilighting(AIS_RotationGizmo.Part.Part_XAxis); } else if (_Gizmo.IsPartDetected(AIS_RotationGizmo.Part.Part_YAxis)) { _RotateMode = RotateMode.AxisY; RotationAxis = new Ax1(_CoordinateSystem.Location, _CoordinateSystem.YDirection); _RotationPlane = new Pln(new Ax3(_CoordinateSystem.Location, _CoordinateSystem.YDirection, _CoordinateSystem.Direction)); _Gizmo.ForcePartHilighting(AIS_RotationGizmo.Part.Part_YAxis); } else if (_Gizmo.IsPartDetected(AIS_RotationGizmo.Part.Part_ZAxis)) { _RotateMode = RotateMode.AxisZ; RotationAxis = new Ax1(_CoordinateSystem.Location, _CoordinateSystem.Direction); _RotationPlane = new Pln(new Ax3(_CoordinateSystem.Location, _CoordinateSystem.Direction, _CoordinateSystem.XDirection)); _Gizmo.ForcePartHilighting(AIS_RotationGizmo.Part.Part_ZAxis); } if (_RotateMode != RotateMode.None) { Pnt resultPnt; if (WorkspaceController.ActiveViewport.ScreenToPoint(_RotationPlane, (int)data.ScreenPoint.X, (int)data.ScreenPoint.Y, out resultPnt)) { var planeDelta = ProjLib.Project(_RotationPlane, resultPnt); _StartValue = Dir2d.DX.Angle(new Dir2d(planeDelta.Coord)); } _AxisHintLine = new HintLine(WorkspaceController, HintStyle.ThinDashed); _AxisHintLine.Set(RotationAxis); WorkspaceController.Invalidate(); WorkspaceController.HudManager?.SetCursor(Cursors.Rotate); return(true); } } return(base.OnMouseDown(data)); }
//-------------------------------------------------------------------------------------------------- public static TopoDS_Shape CreateFacesFromWires(TopoDS_Shape sourceShape, Pln plane) { var wires = sourceShape.Wires(); // Create faces from closed wires var openWireCount = 0; var closedWireCount = 0; var makeFace = new BRepBuilderAPI_MakeFace(plane); foreach (var wire in wires) { var checkWire = new BRepCheck_Wire(wire); if (checkWire.Closed() != BRepCheck_Status.BRepCheck_NoError) { openWireCount++; continue; } makeFace.Add(wire); closedWireCount++; } if (openWireCount > 0) { Messages.Warning("Source shape has " + openWireCount + " unclosed wires, which will be ignored."); } if (closedWireCount == 0) { Messages.Warning("Source shape has no closed wires."); return(new TopoDS_Face()); } if (!makeFace.IsDone()) { Messages.Error("Generating faces from source wires failed."); return(null); } // Fix orientation of that faces var shapeFix = new ShapeFix_Shape(makeFace.Face()); shapeFix.Perform(); return(shapeFix.Shape()); }
public void Serialize_Pln() { var originalValue = new Pln(new Pnt(1.5, 42.3, 11), new Dir(1, 0, -1)); var w = new Writer(); var serializer = Serializer.GetSerializer(typeof(Pln)); Assert.NotNull(serializer); serializer.Write(w, originalValue, null); Assert.True(w.IsValid()); Assert.AreEqual("[1.5,42.3,11,0,0.9238795325112867,0,0.3826834323650898]", w.ToString()); var r = new Reader(w.ToString()); var targetValue = serializer.Read(r, null, null) as Pln?; Assert.False(r.AnyLeft); Assert.True(targetValue.HasValue); Assert.AreEqual(0, originalValue.Distance(targetValue.Value)); }
//-------------------------------------------------------------------------------------------------- IEnumerable <TopoDS_Edge> _CreateProfileEdges(Pln plane, double sx, double sy) { IEnumerable <Geom_Curve> curves = null; switch (_Profile) { case ProfileType.Circle: case ProfileType.HollowCircle: curves = _CreateCircleProfileCurves(plane, sx, sy); break; case ProfileType.Rectangle: case ProfileType.HollowRectangle: curves = _CreateRectangleProfileCurves(plane, sx, sy); break; } return(curves?.Select(curve => new BRepBuilderAPI_MakeEdge(curve).Edge())); }
//-------------------------------------------------------------------------------------------------- IEnumerable <Geom_Curve> _CreateRectangleProfileCurves(Pln plane, double sx, double sy) { if (_Flags.HasFlag(PipeFlags.SymmetricProfile)) { sy = sx; } Trsf tf = new Trsf(plane.Position.Rotated(plane.Axis, _Rotation.ToRad()), Ax3.XOY); Dir dx = Dir.DX.Transformed(tf); Dir dy = Dir.DY.Transformed(tf); yield return(new Geom_TrimmedCurve(new Geom_Line(new Pnt(0, sy, 0).Transformed(tf), dx), -sx, sx)); yield return(new Geom_TrimmedCurve(new Geom_Line(new Pnt(sx, 0, 0).Transformed(tf), dy), -sy, sy)); yield return(new Geom_TrimmedCurve(new Geom_Line(new Pnt(0, -sy, 0).Transformed(tf), dx.Reversed()), -sx, sx)); yield return(new Geom_TrimmedCurve(new Geom_Line(new Pnt(-sx, 0, 0).Transformed(tf), dy.Reversed()), -sy, sy)); }
//-------------------------------------------------------------------------------------------------- public override bool Start() { InteractiveContext.Current.WorkspaceController.Selection.SelectEntity(null, true); if (_InitialCreateMode == CreateMode.Interactive) { var selectionFilter = new OrSelectionFilter(new FaceSelectionFilter(FaceSelectionFilter.FaceType.Plane), new SignatureSelectionFilter(VisualPlane.SelectionSignature)); var toolAction = new SelectSubshapeAction(this, SubshapeTypes.Face, null, selectionFilter); if (!WorkspaceController.StartToolAction(toolAction)) { return(false); } toolAction.Finished += _OnActionFinished; StatusText = "Select face to which the new sketch should be aligned."; WorkspaceController.HudManager?.SetCursor(Cursors.SelectFace); return(true); } switch (_InitialCreateMode) { case CreateMode.WorkplaneXY: _Plane = InteractiveContext.Current.Workspace.WorkingPlane; break; case CreateMode.WorkplaneXZ: _Plane = new Pln(InteractiveContext.Current.Workspace.WorkingPlane.Location, InteractiveContext.Current.Workspace.WorkingPlane.YAxis.Direction.Reversed()); break; case CreateMode.WorkplaneYZ: _Plane = new Pln(InteractiveContext.Current.Workspace.WorkingPlane.Location, InteractiveContext.Current.Workspace.WorkingPlane.XAxis.Direction.Reversed()); break; } Stop(); CreateSketch(); return(false); }
//-------------------------------------------------------------------------------------------------- void _OnActionFinished(ToolAction toolAction) { bool finished = false; var selectAction = toolAction as SelectSubshapeAction; Debug.Assert(selectAction != null); if (selectAction.SelectedEntity is DatumPlane datumPlane) { _Plane = new Pln(datumPlane.GetCoordinateSystem()); finished = true; } else if (selectAction.SelectedSubshapeType == SubshapeTypes.Face) { var face = TopoDS.Face(selectAction.SelectedSubshape); var brepAdaptor = new BRepAdaptor_Surface(face, true); if (brepAdaptor.GetGeomType() != GeomAbs_SurfaceType.GeomAbs_Plane) { StatusText = "Selected face is not a plane type surface."; } else { FaceAlgo.GetCenteredPlaneFromFace(face, out _Plane); finished = true; } } if (finished) { selectAction.Stop(); Stop(); CreateSketch(); } else { selectAction.Reset(); } WorkspaceController.Invalidate(); }
//-------------------------------------------------------------------------------------------------- public static bool GetPlaneOfFaces(TopoDS_Shape shape, out Pln plane) { plane = Pln.XOY; var faces = shape.Faces(); if (faces.Count == 0) { Messages.Error("Cannot get plane of faces, shape doesn't have faces."); return(false); } bool havePlane = false; foreach (var face in faces) { var brepAdaptor = new BRepAdaptor_Surface(face, true); if (brepAdaptor.GetGeomType() != GeomAbs_SurfaceType.GeomAbs_Plane) { Messages.Error("Cannot get plane of faces, shape has faces which are not plane."); return(false); } if (havePlane) { if (!plane.Position.IsCoplanar(brepAdaptor.Plane().Position, 0.00001, 0.00001)) { Messages.Error("Cannot get plane of faces, not all faces are coplanar."); return(false); } } else { plane = brepAdaptor.Plane(); havePlane = true; } } return(true); }