//-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- #region Helper Body _CreateBodies(bool bigsize) { var target = Body.Create(new Box() { DimensionX = 20, DimensionY = 20, DimensionZ = 10, }); target.Position = new Pnt(10, 10, 0); var operands = new IShapeOperand[2]; var body = Body.Create(new Cylinder() { Radius = bigsize ? 20 : 10, Height = 15 }); body.Position = new Pnt(0, 10, 0); operands[0] = new BodyShapeOperand(body); body = Body.Create(new Sphere() { Radius = 15 }); body.Position = new Pnt(10, 10, 5); return(target); }
//-------------------------------------------------------------------------------------------------- public override bool ReplaceOperand(int operandIndex, IShapeOperand operandShape) { if (operandIndex != 1 || Operands.Count < 1 || !(Operands[1] is BodyShapeOperand) || !(operandShape is Shape)) { return(base.ReplaceOperand(operandIndex, operandShape)); } var solidShape = operandShape as Solid; if (solidShape == null) { return(false); } // Transform operandShape var trsf = new Trsf(GetCoordinateSystem(), (Operands[1] as BodyShapeOperand).Body.GetCoordinateSystem()); var bRep = solidShape.GetBRep()?.Moved(new TopLoc_Location(trsf)); if (bRep == null) { return(false); } solidShape.UpdateShape(bRep); // RemoveShape will replace the shape with the predecessor Predecessor = operandShape; Body.RemoveShape(this); return(true); }
//-------------------------------------------------------------------------------------------------- public static (Body target, IShapeOperand[] operands) CreateBooleanBodies(bool bigsize) { var target = Body.Create(new Box() { DimensionX = 20, DimensionY = 20, DimensionZ = 10, }); target.Position = new Pnt(-10, -10, 0); var operands = new IShapeOperand[2]; var body = Body.Create(new Cylinder() { Radius = bigsize ? 20 : 10, Height = 15 }); body.Position = new Pnt(10, 10, 0); operands[0] = new BodyShapeOperand(body); body = Body.Create(new Sphere() { Radius = 15 }); body.Position = new Pnt(-10, -10, 0); operands[1] = new BodyShapeOperand(body); return(target, operands); }
//-------------------------------------------------------------------------------------------------- #endregion #region Operands public bool AddOperand(IShapeOperand operandShape) { Operands.Add(operandShape); operandShape.AddDependent(this); RaisePropertyChanged("ChildCount"); Invalidate(); return(true); }
//-------------------------------------------------------------------------------------------------- public static BooleanFuse Create(Body targetBody, IShapeOperand operand) { Debug.Assert(targetBody != null); var boolean = new BooleanFuse(); targetBody.AddShape(boolean); boolean.AddOperand(operand); return boolean; }
//-------------------------------------------------------------------------------------------------- #endregion #region Invalidation protected override void OnOperandShapeInvalidated(IShapeOperand operand) { // Only invalidate if this comes directly from our associated shape // or our direct predecessor, // since we're not interested in changed in shapes after that. // Also not in the general body notification (e.g. changes to root shape) if (operand != Predecessor && operand != _AssociatedShape) { return; } base.OnOperandShapeInvalidated(operand); }
//-------------------------------------------------------------------------------------------------- bool _MakeSolid(IShapeOperand sourceShape) { var sourceBRep = GetOperandBRep(0); if (sourceBRep == null) { return(false); } // Calculate Parameters Ax3 axis = _CalculateSolidAxis(); var(interval, offset) = _CalculateParameters(); // Build Transformed Shapes TopoDS_Compound resultShape = new TopoDS_Compound(); var builder = new TopoDS_Builder(); builder.MakeCompound(resultShape); for (var index = 0; index < Quantity; index++) { var angle = (interval * index + offset).ToRad(); var transform = Trsf.Identity; if (_KeepOrientation) { // Translation transform transform.SetTranslation(Pnt.Origin.Rotated(axis.Axis, angle).ToVec()); } else { // Rotation transform transform.SetRotation(axis.Axis, angle); } var makeTransform = new BRepBuilderAPI_Transform(sourceBRep, transform); if (!makeTransform.IsDone()) { Messages.Error("Failed transforming shape."); return(false); } builder.Add(resultShape, makeTransform.Shape()); } // Finalize BRep = resultShape; return(true); }
//-------------------------------------------------------------------------------------------------- public static Pipe Create(Body targetBody, IShapeOperand profile = null) { Debug.Assert(targetBody != null); var pipe = new Pipe(); if (profile != null) { pipe.Profile = ProfileType.Custom; pipe.AddOperand(profile); } targetBody.AddShape(pipe); return(pipe); }
//-------------------------------------------------------------------------------------------------- bool _MakeSketch(IShapeOperand sourceShape) { var sourceBRep = GetOperandBRep(0); if (sourceBRep == null) { return(false); } // Calculate Parameters var center = new Pnt2d(-_Radius, 0).Rotated(Pnt2d.Origin, _OriginalAngle.ToRad()); var(interval, offset) = _CalculateParameters(); // Build Transforms List <Trsf2d> transforms = new List <Trsf2d>((int)_Quantity); for (var index = 0; index < _Quantity; index++) { var angle = (interval * index + offset).ToRad(); var transform = Trsf2d.Identity; if (_KeepOrientation) { // Translation transform transform.SetTranslation(Pnt2d.Origin.Rotated(center, angle).ToVec()); } else { // Rotation transform transform.SetRotation(center, angle); } transforms.Add(transform); } // Do it! var resultShape = Topo2dUtils.TransformSketchShape(sourceBRep, transforms, false); if (resultShape == null) { return(false); } // Finalize BRep = resultShape; return(true); }
//-------------------------------------------------------------------------------------------------- public virtual bool ReplaceOperand(int operandIndex, IShapeOperand operandShape) { Debug.Assert(operandIndex < Operands.Count); Debug.Assert(operandShape != null); if (!CanReplaceOperand(operandIndex)) { return(false); } var oldOp = Operands[operandIndex]; oldOp?.RemoveDependent(this); Operands[operandIndex] = operandShape; operandShape.AddDependent(this); RaisePropertyChanged("ChildCount"); Invalidate(); return(true); }
//-------------------------------------------------------------------------------------------------- protected virtual void OnOperandShapeInvalidated(IShapeOperand operand) { Invalidate(); }
//-------------------------------------------------------------------------------------------------- #endregion #region IShapeDependent void IShapeDependent.OnShapeInvalidated(IShapeOperand operand) { OnOperandShapeInvalidated(operand); }
//-------------------------------------------------------------------------------------------------- public bool CanReplaceOperand(IShapeOperand oldOperand) { var operandIndex = Operands.IndexOf(oldOperand); return(CanReplaceOperand(operandIndex)); }
//-------------------------------------------------------------------------------------------------- public bool ReplaceOperand(IShapeOperand oldOperand, IShapeOperand newOperand) { var operandIndex = Operands.IndexOf(oldOperand); return(ReplaceOperand(operandIndex, newOperand)); }
//-------------------------------------------------------------------------------------------------- bool _MakeSolid(IShapeOperand sourceShape) { var sourceBRep = GetOperandBRep(0); if (sourceBRep == null) { return(false); } // Calculate Offsets Ax3 plane; switch (Plane) { case PlaneType.XY: plane = Ax3.XOY.Rotated(Ax1.OZ, _Rotation.ToRad()); break; case PlaneType.ZX: plane = Ax3.ZOX.Rotated(Ax1.OY, _Rotation.ToRad()); break; case PlaneType.YZ: plane = Ax3.YOZ.Rotated(Ax1.OX, _Rotation.ToRad()); break; default: throw new ArgumentOutOfRangeException(); } var bbTransform = new Trsf(new Ax3(Ax2.XOY), plane); var extents = sourceBRep.BoundingBox().Transformed(bbTransform).Extents(); var offset = Vec.Zero; var interval1 = plane.XDirection.ToVec() * _CalculateOffset(DistanceMode1, Quantity1, Distance1, extents.X); switch (_Alignment1) { case AlignmentMode.Center: offset += interval1 * (Quantity1 - 1) * -0.5; break; case AlignmentMode.Last: interval1 *= -1; break; } var interval2 = plane.YDirection.ToVec() * _CalculateOffset(DistanceMode2, Quantity2, Distance2, extents.Y); switch (_Alignment2) { case AlignmentMode.Center: offset += interval2 * (Quantity2 - 1) * -0.5; break; case AlignmentMode.Last: interval2 *= -1; break; } // Build Transformed Shapes TopoDS_Compound resultShape = new TopoDS_Compound(); var builder = new TopoDS_Builder(); builder.MakeCompound(resultShape); for (var index1 = 0; index1 < Quantity1; index1++) { for (var index2 = 0; index2 < Quantity2; index2++) { if (_Border && index1 != 0 && index1 != Quantity1 - 1 && index2 != 0 && index2 != Quantity2 - 1) { continue; // Skip inner parts } var transform = new Trsf(); transform.SetTranslation(interval1 * index1 + interval2 * index2 + offset); var makeTransform = new BRepBuilderAPI_Transform(sourceBRep, transform); if (!makeTransform.IsDone()) { Messages.Error("Failed transforming shape."); return(false); } builder.Add(resultShape, makeTransform.Shape()); } } // Finalize BRep = resultShape; return(true); }
//-------------------------------------------------------------------------------------------------- #endregion #region IShapeDependent implementation public void OnShapeInvalidated(IShapeOperand operand) { Invalidate(); }
//-------------------------------------------------------------------------------------------------- bool _MakeSketch(IShapeOperand sourceShape) { var sourceBRep = GetOperandBRep(0); if (sourceBRep == null) { return(false); } // Calculate Offsets var extents = sourceBRep.BoundingBox().Extents(); var interval1 = new Vec2d(_CalculateOffset(DistanceMode1, Quantity1, Distance1, extents.X), 0); interval1.Rotate(_Rotation.ToRad()); var offset = Vec2d.Zero; switch (_Alignment1) { case AlignmentMode.Center: offset += interval1 * (Quantity1 - 1) * -0.5; break; case AlignmentMode.Last: interval1 *= -1; break; } var interval2 = new Vec2d(0, _CalculateOffset(DistanceMode2, Quantity2, Distance2, extents.Y)); interval2.Rotate(_Rotation.ToRad()); switch (_Alignment2) { case AlignmentMode.Center: offset += interval2 * (Quantity2 - 1) * -0.5; break; case AlignmentMode.Last: interval2 *= -1; break; } // Build Transforms List <Trsf2d> transforms = new List <Trsf2d>((int)(Quantity1 * Quantity2)); for (var index1 = 0; index1 < Quantity1; index1++) { for (var index2 = 0; index2 < Quantity2; index2++) { if (_Border && index1 != 0 && index1 != Quantity1 - 1 && index2 != 0 && index2 != Quantity2 - 1) { continue; // Skip inner parts } var transform = new Trsf2d(); transform.SetTranslation(interval1 * index1 + interval2 * index2 + offset); transforms.Add(transform); } } // Do it! var resultShape = Topo2dUtils.TransformSketchShape(sourceBRep, transforms, false); if (resultShape == null) { return(false); } // Finalize BRep = resultShape; return(true); }