Example #1
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);
        }
Example #2
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
            });
        }