public void SpinSortValidation() { var vector = new CorrelationVectorV2(); var spinParameters = new SpinParameters { Entropy = SpinEntropy.Two, Interval = SpinCounterInterval.Fine, Periodicity = SpinCounterPeriodicity.Short }; uint lastSpinValue = 0; var wrappedCounter = 0; for (int i = 0; i < 100; i++) { // The cV after a Spin will look like <cvBase>.0.<spinValue>.0, so the spinValue is at index = 2. var spinValue = uint.Parse(CorrelationVector.Spin(vector.Value, spinParameters).Value.Split('.')[2]); // Count the number of times the counter wraps. if (spinValue <= lastSpinValue) { wrappedCounter++; } lastSpinValue = spinValue; // Wait for 10ms. Task.Delay(10).Wait(); } // The counter should wrap at most 1 time. Assert.IsTrue(wrappedCounter <= 1); }
/// <summary> /// Create thread of base of screw /// </summary> /// <returns>true if operation successful; false in case of error</returns> private bool CreateThread(ksEntity[] carvingEntities) { var coef = 0.7; var gostspin = 0.037; var endX = _kompasApp.Parameters[2] + _kompasApp.Parameters[3] + (_kompasApp.Parameters[4] * 0.86); var startY = -(_kompasApp.Parameters[0] * coef / 2.0); var endY = -(3.0 / 4.0 * _kompasApp.Parameters[0] * coef) / 2.0; var spinParameters = new SpinParameters { Document3DPart = _kompasApp.ScrewPart, BeginSpinFace = carvingEntities[1], EndSpinFace = carvingEntities[0], SpinLocationPoint = new KompasPoint2D(0, 0), DiameterSize = _kompasApp.Parameters[0] * coef, SpinStep = _kompasApp.Parameters[3] * gostspin }; var screwThreadSpin = new Spin(spinParameters); var step = screwThreadSpin.SpinStep; var screwThreadSketch = new KompasSketch(_kompasApp.ScrewPart, Obj3dType.o3d_planeXOZ); var screwThreadEdit = screwThreadSketch.BeginEntityEdit(); screwThreadEdit.ksLineSeg(endX - step, endY, endX, endY, 1); screwThreadEdit.ksLineSeg(endX, endY, endX - step / 2.0, startY, 1); screwThreadEdit.ksLineSeg(endX - step / 2.0, startY, endX - step, endY, 1); screwThreadSketch.EndEntityEdit(); var spinCollection = (ksEntityCollection)_kompasApp.ScrewPart.EntityCollection( (short)Obj3dType.o3d_cylindricSpiral); spinCollection.Clear(); spinCollection.Add(screwThreadSpin.Entity); spinCollection.refresh(); if (spinCollection.GetCount() != 1) { LastErrorCode = ErrorCodes.EntityCollectionWrong; } var extrusionParameters = new KompasExtrusionParameters(_kompasApp.ScrewPart, Obj3dType.o3d_baseEvolution, screwThreadSketch.Entity, spinCollection); var screwThreadExtrusion = new KompasExtrusion( extrusionParameters, ExtrusionType.BySketchesCollection); return(true); }
/// <summary> /// Create thread of base of screw /// </summary> /// <returns>true if operation successful; false in case of error</returns> private bool CreateThread(ksEntity[] carvingEntities) { // 1.5 Screw base thread spin // Spin step by russian GOST is equal to 0.037 (i.e. 3.7%) of spin height var spinParameters = new SpinParameters(); spinParameters.Document3DPart = _kompasApp.ScrewPart; spinParameters.BeginSpinFace = carvingEntities[1]; spinParameters.EndSpinFace = carvingEntities[0]; spinParameters.SpinLocationPoint = new KompasPoint2D(0, 0); spinParameters.DiameterSize = _kompasApp.Parameters[0] * 0.7; // 0.7 W3 spinParameters.SpinStep = _kompasApp.Parameters[3] * 0.037; // 0.037 W2 var screwThreadSpin = new Spin(spinParameters); if (screwThreadSpin.LastErrorCode != ErrorCodes.OK) { LastErrorCode = screwThreadSpin.LastErrorCode; return(false); } ThreadStep = screwThreadSpin.SpinStep; if (!DoubleValidator.Validate(ThreadStep)) { LastErrorCode = ErrorCodes.DoubleValueValidationError; return(false); } // 1.6 Screw base thread sketch var screwThreadSketch = new KompasSketch(_kompasApp.ScrewPart, Obj3dType.o3d_planeXOZ); if (screwThreadSketch.LastErrorCode != ErrorCodes.OK) { LastErrorCode = screwThreadSketch.LastErrorCode; return(false); } var screwThreadEdit = screwThreadSketch.BeginEntityEdit(); if (screwThreadEdit == null) { LastErrorCode = screwThreadSketch.LastErrorCode; return(false); } var step = screwThreadSpin.SpinStep; // W1 + W2 + 3 thread steps - 0.86 * W3 (because part with 0.16 * W3 is in coordinates which are less than zero of XOZ) var endX = _kompasApp.Parameters[2] + _kompasApp.Parameters[3] + _kompasApp.Parameters[4] * 0.86; var startY = -(_kompasApp.Parameters[0] * 0.7 / 2.0); // 0.7 * W3 var endY = -(3.0 / 4.0 * _kompasApp.Parameters[0] * 0.7) / 2.0; // 0.7 * W3 on end of base // Draw triangle: the base of thread. screwThreadEdit.ksLineSeg(endX - step, endY, endX, endY, 1); screwThreadEdit.ksLineSeg(endX, endY, endX - step / 2.0, startY, 1); screwThreadEdit.ksLineSeg(endX - step / 2.0, startY, endX - step, endY, 1); screwThreadSketch.EndEntityEdit(); // 1.7 Screw base thread extrusion var spinCollection = (ksEntityCollection)_kompasApp.ScrewPart.EntityCollection((short)Obj3dType.o3d_cylindricSpiral); if (spinCollection == null) { LastErrorCode = ErrorCodes.EntityCollectionCreateError; return(false); } spinCollection.Clear(); spinCollection.Add(screwThreadSpin.Entity); spinCollection.refresh(); if (spinCollection.GetCount() != 1) { LastErrorCode = ErrorCodes.EntityCollectionWrong; } var extrusionParameters = new KompasExtrusionParameters(_kompasApp.ScrewPart, Obj3dType.o3d_baseEvolution, screwThreadSketch.Entity, spinCollection); var screwThreadExtrusion = new KompasExtrusion(extrusionParameters, ExtrusionType.BySketchesCollection); if (screwThreadExtrusion.LastErrorCode != ErrorCodes.OK) { LastErrorCode = ErrorCodes.EntityCreateError; return(false); } return(true); }
/// <summary> /// Create nut thread /// </summary> /// Creating inner thread tooth: /// endZZ /// A------=------B •–→X /// |\ | /| ↓ /// | \ | / | Z /// | \ | / | /// startX | \ | / | endX /// | \ | / | /// | \|/ | /// |------C------| /// startZ /// /// /// Here offsetX is (W1 + W2 + H + 0.1*W1*W2 (i.e. _basePlaneCylinderDepth) - 0.68 * H (i.e. nut height minus heights of right and left chamfers) /// (chamfer is 0.16 of neight of nut) /// /// threadStartX = offsetX - step; /// threadEndX = offsetX; /// /// threadEndZ = 1/2 * (0.7 * W3) /// threadStartZ = 1/2 * (3/4 * 0.7 * W3) /// /// A = (threadStartX; threadEndZ); /// B = (threadEndX; threadEndZ); /// C = (threadStartX + step/2; threadStartZ); /// <param name="chamferEntities">Chamfer entities from left and right sides of nut</param> /// <param name="nutBasePoint">Nut base point</param> /// <param name="basePlaneCylinderDepth">Depth of cylinder of base plane (see <seealso cref="CreateDetail"></seealso> for more info)</param> /// <returns>true if operation successful; false in case of error</returns> private bool CreateNutThread(ksEntity[] chamferEntities, KompasPoint3D nutBasePoint, double basePlaneCylinderDepth) { // 1.6 Nut base thread spin var spinParameters = new SpinParameters(); spinParameters.Document3DPart = _kompasApp.NutPart; spinParameters.BeginSpinFace = chamferEntities[1]; spinParameters.EndSpinFace = chamferEntities[0]; spinParameters.SpinLocationPoint = new KompasPoint2D(nutBasePoint.Y, nutBasePoint.Z); spinParameters.DiameterSize = _kompasApp.Parameters[5]; spinParameters.SpinStep = _kompasApp.ThreadStep; var nutThreadSpin = new Spin(spinParameters); // D if (nutThreadSpin.LastErrorCode != ErrorCodes.OK) { LastErrorCode = nutThreadSpin.LastErrorCode; return(false); } // 1.7 Nut base thread sketch var nutThreadSketch = new KompasSketch(_kompasApp.NutPart, Obj3dType.o3d_planeXOZ); if (nutThreadSketch.LastErrorCode != ErrorCodes.OK) { LastErrorCode = nutThreadSketch.LastErrorCode; return(false); } var nutThreadEdit = nutThreadSketch.BeginEntityEdit(); // See comment on top of the function // 0.18 is 0.16 plus some extra space: 0.02. var offsetX = basePlaneCylinderDepth + _kompasApp.Parameters[4] * 0.18; var step = _kompasApp.ThreadStep; var threadStartX = offsetX - step; var threadEndX = offsetX; var threadStartZ = 1.0 / 2.0 * (_kompasApp.Parameters[0] * 0.7); // 1/2 * (0.7 * W3) var threadEndZ = 1.0 / 2.0 * (_kompasApp.Parameters[0] * 0.7 * (3.0 / 4.0)); // 1/2 * (3/4 * 0.7 * W3) // A = (threadStartX; threadEndZ); // B = (threadEndX; threadEndZ); // C = (threadStartX + step/2; threadStartZ); nutThreadEdit.ksLineSeg(threadStartX, threadEndZ, threadEndX, threadEndZ, 1); // AB nutThreadEdit.ksLineSeg(threadStartX, threadEndZ, threadStartX + step / 2.0, threadStartZ, 1); // AC nutThreadEdit.ksLineSeg(threadStartX + step / 2.0, threadStartZ, threadEndX, threadEndZ, 1); // CB nutThreadSketch.EndEntityEdit(); // 1.8 Nut base spin creation var spinCollection = (ksEntityCollection)_kompasApp.ScrewPart.EntityCollection((short)Obj3dType.o3d_cylindricSpiral); spinCollection.Clear(); spinCollection.Add(nutThreadSpin.Entity); spinCollection.refresh(); var extrusionParameters = new KompasExtrusionParameters(_kompasApp.NutPart, Obj3dType.o3d_cutEvolution, nutThreadSketch.Entity, spinCollection); var nutBaseSpin = new KompasExtrusion(extrusionParameters, ExtrusionType.BySketchesCollection); if (nutBaseSpin.LastErrorCode != ErrorCodes.OK) { LastErrorCode = nutBaseSpin.LastErrorCode; return(false); } return(true); }