Exemplo n.º 1
0
    //--------------------------------------------------------------------------------------------------
    //--------------------------------------------------------------------------------------------------


    #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);
    }
Exemplo n.º 2
0
        //--------------------------------------------------------------------------------------------------

        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);
        }
Exemplo n.º 3
0
        //--------------------------------------------------------------------------------------------------

        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);
        }
Exemplo n.º 4
0
        //--------------------------------------------------------------------------------------------------

        #endregion

        #region Operands

        public bool AddOperand(IShapeOperand operandShape)
        {
            Operands.Add(operandShape);
            operandShape.AddDependent(this);

            RaisePropertyChanged("ChildCount");
            Invalidate();

            return(true);
        }
Exemplo n.º 5
0
        //--------------------------------------------------------------------------------------------------

        public static BooleanFuse Create(Body targetBody, IShapeOperand operand)
        {
            Debug.Assert(targetBody != null);

            var boolean = new BooleanFuse();
            targetBody.AddShape(boolean);
            boolean.AddOperand(operand);

            return boolean;
        }
Exemplo n.º 6
0
        //--------------------------------------------------------------------------------------------------

        #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);
        }
Exemplo n.º 8
0
        //--------------------------------------------------------------------------------------------------

        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);
        }
Exemplo n.º 10
0
        //--------------------------------------------------------------------------------------------------

        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);
        }
Exemplo n.º 11
0
        //--------------------------------------------------------------------------------------------------

        protected virtual void OnOperandShapeInvalidated(IShapeOperand operand)
        {
            Invalidate();
        }
Exemplo n.º 12
0
        //--------------------------------------------------------------------------------------------------

        #endregion

        #region IShapeDependent

        void IShapeDependent.OnShapeInvalidated(IShapeOperand operand)
        {
            OnOperandShapeInvalidated(operand);
        }
Exemplo n.º 13
0
        //--------------------------------------------------------------------------------------------------

        public bool CanReplaceOperand(IShapeOperand oldOperand)
        {
            var operandIndex = Operands.IndexOf(oldOperand);

            return(CanReplaceOperand(operandIndex));
        }
Exemplo n.º 14
0
        //--------------------------------------------------------------------------------------------------

        public bool ReplaceOperand(IShapeOperand oldOperand, IShapeOperand newOperand)
        {
            var operandIndex = Operands.IndexOf(oldOperand);

            return(ReplaceOperand(operandIndex, newOperand));
        }
Exemplo n.º 15
0
        //--------------------------------------------------------------------------------------------------

        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();
        }
Exemplo n.º 17
0
        //--------------------------------------------------------------------------------------------------

        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);
        }