Exemple #1
0
        public void TestRegPolyParameterNormal(ErrorCodes errorCode, int anglesCount, double inscribedCircleRadius)
        {
            var appTest = new KompasApplicationTest();
            var app     = appTest.CreateKompasApplication();

            var sketch     = new KompasSketch(app.ScrewPart, Obj3dType.o3d_planeXOZ);
            var sketchEdit = sketch.BeginEntityEdit();

            var rectangleParam = new RegularPolygonParameter(app, anglesCount, inscribedCircleRadius, new KompasPoint2D(0.0, 0.0));

            sketchEdit.ksRectangle(rectangleParam.FigureParam);
            sketch.EndEntityEdit();

            Assert.AreEqual(rectangleParam.LastErrorCode, errorCode);
        }
        /// <summary>
        /// Метод построения щлица в виде многоугольника
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        protected ksEntity CreateCut(double[] parameters)
        {
            var offsetX = parameters[0];
            var offsetY = parameters[1];

            var width = parameters[2];

            var regPolySketch     = new KompasSketch(_kompasApp.ScrewPart, Obj3dType.o3d_planeYOZ);
            var regPolySketchEdit = regPolySketch.BeginEntityEdit();
            var regPolyPoint      = new KompasPoint2D(offsetX, offsetY);

            var regPolyParam = new RegularPolygonParameter(_kompasApp, 6, width, regPolyPoint);

            if (regPolySketchEdit.ksRegularPolygon(regPolyParam.FigureParam, 0) == 0)
            {
                LastErrorCode = ErrorCodes.Document2DRegPolyCreateError;
                return(null);
            }

            regPolySketch.EndEntityEdit();

            var gost = 0.84;
            var extrusionParameters = new KompasExtrusionParameters
                                      (
                _kompasApp.ScrewPart,
                Obj3dType.o3d_cutExtrusion,
                regPolySketch.Entity,
                Direction_Type.dtNormal,
                _kompasApp.Parameters[1] * gost
                                      );

            var regPolyExtrusion = new KompasExtrusion(
                extrusionParameters, ExtrusionType.ByEntity);

            return(regPolyExtrusion.ExtrudedEntity);
        }
Exemple #3
0
        /// <summary>
        /// Create screw hat with extrusion operation
        /// </summary>
        /// "regPoly" here is abbreviation of Regular Polygon
        /// Screw hat consists of base of hat (0.84 * H)
        /// and rounded chamfer on the top (0.16 * H)
        /// <returns>Extruded entity of hat for base part of screw</returns>
        private ksEntity CreateHat()
        {
            // 0.1 Create muffler, which base point is (W3 / 5, W3 / 5)
            var basePoint         = -(_kompasApp.Parameters[0] / 5.0);
            var mufflerParameters = new MufflerParameters();

            mufflerParameters.Document3DPart = _kompasApp.ScrewPart;
            mufflerParameters.Direction      = Direction_Type.dtNormal;
            mufflerParameters.BasePlaneAxis  = Obj3dType.o3d_planeYOZ;
            mufflerParameters.BasePlanePoint = new KompasPoint2D(basePoint, basePoint);

            var mufflerManager = new Muffler(_kompasApp, mufflerParameters);

            if (mufflerManager.LastErrorCode != ErrorCodes.OK)
            {
                LastErrorCode = mufflerManager.LastErrorCode;
                return(null);
            }

            var mufflerExtrusion = mufflerManager.Extrusion;

            if (mufflerExtrusion == null)
            {
                LastErrorCode = mufflerManager.LastErrorCode;
                return(null);
            }

            // 1.1 Hat sketch
            var regPolySketch = new KompasSketch(_kompasApp.ScrewPart, Obj3dType.o3d_planeYOZ);

            if (regPolySketch.LastErrorCode != ErrorCodes.OK)
            {
                LastErrorCode = regPolySketch.LastErrorCode;
                return(null);
            }

            var regPolySketchEdit = regPolySketch.BeginEntityEdit();

            if (regPolySketchEdit == null)
            {
                LastErrorCode = regPolySketch.LastErrorCode;
                return(null);
            }

            // Regular polygon center point
            var regPolyPoint = new KompasPoint2D(0, 0);
            // Regular polygon is base of hat
            var regPolyParam = new RegularPolygonParameter(_kompasApp, 6, _kompasApp.Parameters[0] / 2.0, regPolyPoint);              // W3

            if (regPolySketchEdit.ksRegularPolygon(regPolyParam.FigureParam, 0) == 0)
            {
                LastErrorCode = ErrorCodes.Document2DRegPolyCreatingError;
                return(null);
            }

            regPolySketch.EndEntityEdit();

            // 1.2 Hat entity extrusion
            // Screw hat height is equal to nut height
            var extrusionParameters = new KompasExtrusionParameters(_kompasApp.ScrewPart, Obj3dType.o3d_baseExtrusion, regPolySketch.Entity, Direction_Type.dtReverse, _kompasApp.Parameters[4] * 0.84);
            var regPolyExtrusion    = new KompasExtrusion(extrusionParameters, ExtrusionType.ByEntity);          // 0.84 * H

            if (regPolyExtrusion.LastErrorCode != ErrorCodes.OK)
            {
                LastErrorCode = regPolyExtrusion.LastErrorCode;
                return(null);
            }

            /* Main base face area is lower than parallel base face
             * because of muffler partially overlaps main base face area,
             * but not overlaps parallel base face area.
             */
            regPolyExtrusion.BaseFaceAreaState = KompasFaces.BaseFaceAreaState.BaseFaceAreaLower;
            var extruded = regPolyExtrusion.ExtrudedEntity;

            if (extruded == null)
            {
                LastErrorCode = regPolyExtrusion.LastErrorCode;
                return(null);
            }

            // 0.2 Delete muffler
            if (!mufflerManager.DeleteDetail())
            {
                LastErrorCode = mufflerManager.LastErrorCode;
                return(null);
            }

            // 1.3 Rounded chamfer in hat
            var roundedChamferParameters = new RoundedChamferParameters();

            roundedChamferParameters.Document3DPart           = _kompasApp.ScrewPart;
            roundedChamferParameters.RegularPolygonSketch     = regPolySketch.Entity;
            roundedChamferParameters.RegularPolygonParameters = regPolyParam;
            roundedChamferParameters.BasePlanePoint           = new KompasPoint2D(0.0, 0.0);
            roundedChamferParameters.Direction = Direction_Type.dtNormal;
            var roundedChamferManager = new RoundedChamfer(_kompasApp, roundedChamferParameters);

            if (!roundedChamferManager.CreateDetail())
            {
                LastErrorCode = roundedChamferManager.LastErrorCode;
                return(null);
            }

            return(extruded);
        }
Exemple #4
0
        /// <summary>
        /// Create nut
        /// </summary>
        /// <returns>true if operation successful, false in case of error</returns>
        public bool CreateDetail()
        {
            // Base point of nut in three-dimensional coordinate system
            var nutBasePoint = new KompasPoint3D(0.0, Math.Abs(_kompasApp.Parameters[0]), Math.Abs(_kompasApp.Parameters[0]));             // / nut placement /

            // Depth of cylinder which sets base plane
            // Nut base point is (0;0) in YOZ after nut base plane creation
            var regPolyParam = new RegularPolygonParameter(_kompasApp, 6, _kompasApp.Parameters[0] / 2.0, new KompasPoint2D(0.0, 0.0));

            if (regPolyParam.LastErrorCode != ErrorCodes.OK)
            {
                LastErrorCode = regPolyParam.LastErrorCode;
                return(false);
            }

            // Base plane cylinder depth is W1 + W2 + H + 0.1*W1*W2
            var basePlaneCylinderDepth = _kompasApp.Parameters[2] + _kompasApp.Parameters[3] + _kompasApp.Parameters[4] + (_kompasApp.Parameters[2] + _kompasApp.Parameters[3]) * 0.1;

            if (!DoubleValidator.Validate(basePlaneCylinderDepth))
            {
                LastErrorCode = ErrorCodes.DoubleValueValidationError;
                return(false);
            }

            // 1. Create nut base plane cylinder (base plane is plane in three-dimensional coordinate system, not an entity!)
            var nutBasePlane = CreateNutBasePlaneCylinder(nutBasePoint, basePlaneCylinderDepth);

            if (nutBasePlane == null)
            {
                LastErrorCode = ErrorCodes.ArgumentNull;
                return(false);
            }

            // Nut base point is (0;0) in YOZ after nut base plane creation
            nutBasePoint.Y = 0.0;
            nutBasePoint.Z = 0.0;

            // 2. Create nut base
            var nutBaseEntities = CreateNutBase(nutBasePlane, nutBasePoint, regPolyParam);

            if (nutBaseEntities == null ||
                nutBaseEntities[0] == null ||
                nutBaseEntities[1] == null
                )
            {
                return(false);
            }

            // 3. Delete nut base plane cylinder
            if (!DeleteNutBasePlaneCylinder(nutBasePlane, basePlaneCylinderDepth))
            {
                return(false);
            }

            // 4. Create nut chamfer entities
            var chamferEntities = CreateNutChamferEntities(nutBasePoint, regPolyParam, nutBaseEntities);

            if (chamferEntities == null ||
                chamferEntities[0] == null ||
                chamferEntities[1] == null
                )
            {
                return(false);
            }

            // 5. Create nut base cut
            if (!CreateBaseCut(chamferEntities, nutBasePoint))
            {
                return(false);
            }

            // 6. Create nut thread
            if (!CreateNutThread(chamferEntities, nutBasePoint, basePlaneCylinderDepth))
            {
                return(false);
            }

            return(true);
        }
Exemple #5
0
        /// <summary>
        /// Create rounded chamfers inside nut
        /// </summary>
        /// <param name="nutBasePoint">Nut base point</param>
        /// <param name="regPolyParam">Regular polygon parameters</param>
        /// <param name="nutBaseEntities">Nut base entities: sketch and extrusion</param>
        /// <returns>Chamfers entities from left and right sides of nut</returns>
        private ksEntity[] CreateNutChamferEntities(KompasPoint3D nutBasePoint, RegularPolygonParameter regPolyParam, ksEntity[] nutBaseEntities)
        {
            // 1.3 Nut rounded chamfers creation:
            // 1.3.1 Right rounded chamfer
            var rightChamferPoint = new KompasPoint2D(Math.Abs(nutBasePoint.Y), Math.Abs(nutBasePoint.Z));

            // Nut width is equal to W3 (screw hat width)
            var rightChamferRegPoly = new RegularPolygonParameter(_kompasApp, 6, _kompasApp.Parameters[0] / 2.0, rightChamferPoint);

            if (rightChamferRegPoly == null)
            {
                LastErrorCode = rightChamferRegPoly.LastErrorCode;
                return(null);
            }

            var rightChamferSketch = new KompasSketch(_kompasApp.NutPart, nutBaseEntities[1]);

            if (rightChamferSketch.LastErrorCode != ErrorCodes.OK)
            {
                LastErrorCode = rightChamferSketch.LastErrorCode;
                return(null);
            }

            var rightChamferParameters = new RoundedChamferParameters();

            rightChamferParameters.Document3DPart           = _kompasApp.NutPart;
            rightChamferParameters.RegularPolygonSketch     = rightChamferSketch.Entity;
            rightChamferParameters.RegularPolygonParameters = rightChamferRegPoly;
            rightChamferParameters.BasePlanePoint           = rightChamferPoint;
            rightChamferParameters.Direction = Direction_Type.dtNormal;

            var rightChamferManager = new RoundedChamfer(_kompasApp, rightChamferParameters);

            if (!rightChamferManager.CreateDetail())
            {
                LastErrorCode = rightChamferManager.LastErrorCode;
                return(null);
            }

            if (rightChamferManager.LastErrorCode != ErrorCodes.OK)
            {
                LastErrorCode = rightChamferManager.LastErrorCode;
                return(null);
            }

            var rightChamferEntity = rightChamferManager.Entity;

            if (rightChamferEntity == null)
            {
                LastErrorCode = rightChamferManager.LastErrorCode;
                return(null);
            }

            // 1.3.2 Left rounded chamfer
            var leftChamferParameters = new RoundedChamferParameters();

            leftChamferParameters.Document3DPart           = _kompasApp.NutPart;
            leftChamferParameters.RegularPolygonSketch     = nutBaseEntities[0];
            leftChamferParameters.RegularPolygonParameters = regPolyParam;
            leftChamferParameters.BasePlanePoint           = new KompasPoint2D(nutBasePoint.Y, nutBasePoint.Z);
            leftChamferParameters.Direction = Direction_Type.dtNormal;

            var leftChamferManager = new RoundedChamfer(_kompasApp, leftChamferParameters);

            if (!leftChamferManager.CreateDetail())
            {
                LastErrorCode = leftChamferManager.LastErrorCode;
                return(null);
            }

            if (leftChamferManager.LastErrorCode != ErrorCodes.OK)
            {
                LastErrorCode = leftChamferManager.LastErrorCode;
                return(null);
            }

            var leftChamferEntity = leftChamferManager.Entity;

            if (leftChamferEntity == null)
            {
                LastErrorCode = leftChamferManager.LastErrorCode;
                return(null);
            }

            return(new ksEntity[2] {
                leftChamferEntity, rightChamferEntity
            });
        }
Exemple #6
0
        /// <summary>
        /// Create nut base extrusion and sketch entities
        /// </summary>
        /// <param name="nutBasePlane">Base plane (see <seealso cref="CreateDetail"></seealso> for more info)</param>
        /// <param name="nutBasePoint">Nut base point</param>
        /// <param name="regPolyParam">Regular polygon parameters</param>
        /// <returns>
        /// An array of nut base sketch and extrusion entities
        /// or null in case of error during creation
        /// </returns>
        private ksEntity[] CreateNutBase(ksEntity nutBasePlane, KompasPoint3D nutBasePoint, RegularPolygonParameter regPolyParam)
        {
            // 1.1 Nut base sketch
            var nutBaseSketch = new KompasSketch(_kompasApp.NutPart, nutBasePlane);

            if (nutBaseSketch.LastErrorCode != ErrorCodes.OK)
            {
                LastErrorCode = nutBaseSketch.LastErrorCode;
                return(null);
            }

            var nutBaseEdit = nutBaseSketch.BeginEntityEdit();

            if (nutBaseEdit == null)
            {
                LastErrorCode = nutBaseSketch.LastErrorCode;
                return(null);
            }

            // Nut width is equal to W3 (screw hat width)
            if (nutBaseEdit.ksRegularPolygon(regPolyParam.FigureParam, 1) == 0)
            {
                LastErrorCode = ErrorCodes.Document2DRegPolyCreatingError;
                return(null);
            }

            nutBaseSketch.EndEntityEdit();

            // 1.2 Nut base extrusion
            var extrusionParameters = new KompasExtrusionParameters(_kompasApp.NutPart, Obj3dType.o3d_baseExtrusion, nutBaseSketch.Entity, Direction_Type.dtReverse, _kompasApp.Parameters[4] / 2.0);
            var nutBaseExtrusion    = new KompasExtrusion(extrusionParameters, ExtrusionType.ByEntity);           // / H /

            if (nutBaseExtrusion.LastErrorCode != ErrorCodes.OK)
            {
                LastErrorCode = nutBaseExtrusion.LastErrorCode;
                return(null);
            }

            // 0.1 Create muffler
            var mufflerParameters = new MufflerParameters();

            mufflerParameters.Document3DPart = _kompasApp.NutPart;
            mufflerParameters.Direction      = Direction_Type.dtNormal;
            mufflerParameters.BasePlaneAxis  = Obj3dType.o3d_planeXOZ;
            mufflerParameters.BasePlanePoint = new KompasPoint2D(nutBasePoint.Y, nutBasePoint.Z);

            var muffler = new Muffler(_kompasApp, mufflerParameters, nutBaseSketch.Entity);

            if (muffler.LastErrorCode != ErrorCodes.OK)
            {
                LastErrorCode = muffler.LastErrorCode;
                return(null);
            }

            // 1.3 Get base plane
            nutBaseExtrusion.BaseFaceAreaState = KompasFaces.BaseFaceAreaState.BaseFaceAreaLower;
            var nutBaseExtrudedEntity = nutBaseExtrusion.ExtrudedEntity;

            if (nutBaseExtrudedEntity == null)
            {
                LastErrorCode = nutBaseExtrusion.LastErrorCode;
                return(null);
            }

            // 0.2 Delete muffler
            if (!muffler.DeleteDetail())
            {
                LastErrorCode = muffler.LastErrorCode;
                return(null);
            }

            return(new ksEntity[2] {
                nutBaseSketch.Entity, nutBaseExtrudedEntity
            });
        }