enum RxHoleType { PinHole, NSmoothHole, ISmoothHole, ThreadHole }//PinHole销孔 NormarlSmoothHole 光孔 Inner SmoothHole 沉头孔 ThreadHole 螺纹孔 /// <summary> /// 新增孔 /// </summary> /// <param name="UserSelected">用户选择的3个参考</param> /// <param name="tform">父级窗体</param> /// <param name="AbortHole">是否对装配零件直接打孔</param> public void CreateNwThroughtHole(Reference[] UserSelected, Form tform, bool AbortHole, object[] UserSelectedValue) { object[] HolePoint = new object[3]; Edge ed1 = (Edge)UserSelected[0]; Edge ed2 = (Edge)UserSelected[1]; if (CatActiveDoc == null) { InitCatEnv(tform); } SPAWorkbench TheSPAWorkbench = (SPAWorkbench)CatActiveDoc.GetWorkbench("SPAWorkbench"); Measurable LengthA = TheSPAWorkbench.GetMeasurable(ed1); Measurable LengthB = TheSPAWorkbench.GetMeasurable(ed2); Edge LengthEdge = LengthA.Length >= LengthB.Length ? ed1 : ed2; //获取长边 Edge WeightEdge = LengthA.Length <= LengthB.Length ? ed1 : ed2; //获取短边 double Weight = LengthA.Length <= LengthB.Length ? LengthA.Length : LengthB.Length; double Length = LengthA.Length >= LengthB.Length ? LengthA.Length : LengthB.Length; double JianDis = 0; if (Length <= 45) { JianDis = 12.5; } else if (Length <= 60 && Length > 45) { JianDis = 15; } else if (Length > 60) { JianDis = 15; } GetProductByFace GPB = new GetProductByFace(); Part Spart = GPB.GetPart((Face)UserSelected[2]); ShapeFactory shapeFactory = (ShapeFactory)Spart.ShapeFactory; Hole Nwhole = shapeFactory.AddNewHoleWith2Constraints(Convert.ToDouble(HolePoint[0]), Convert.ToDouble(HolePoint[1]), Convert.ToDouble(HolePoint[2]), UserSelected[0], UserSelected[1], UserSelected[2], 50);//创建过孔 RxSetHoleType(Nwhole, 10, Weight / 2, 9, RxHoleType.NSmoothHole); Spart.Update(); Hole NwPinHole = shapeFactory.AddNewHoleWith2Constraints(Convert.ToDouble(HolePoint[0]) + 5, Convert.ToDouble(HolePoint[1]) + 5, Convert.ToDouble(HolePoint[2]), UserSelected[0], UserSelected[1], UserSelected[2], 50);//创建销孔 RxSetHoleType(NwPinHole, 10, Weight / 2 + JianDis, 8, RxHoleType.PinHole); Spart.Update(); if (Length <= 60) { RectPattern RPhole = shapeFactory.AddNewRectPattern(Nwhole, 2, 1, JianDis * 2, 20, 1, 1, LengthEdge, WeightEdge, true, true, 0); //阵列过孔 RPhole.FirstRectangularPatternParameters = CatRectangularPatternParameters.catInstancesandSpacing; RPhole.SecondRectangularPatternParameters = CatRectangularPatternParameters.catInstancesandSpacing; Spart.Update(); } else { RectPattern RPhole = shapeFactory.AddNewRectPattern(Nwhole, 2, 1, JianDis * 2, 20, 1, 1, LengthEdge, WeightEdge, true, true, 0); //阵列过孔 RPhole.FirstRectangularPatternParameters = CatRectangularPatternParameters.catInstancesandSpacing; RPhole.SecondRectangularPatternParameters = CatRectangularPatternParameters.catInstancesandSpacing; RectPattern RPinhole = shapeFactory.AddNewRectPattern(NwPinHole, 2, 1, JianDis * 2, 20, 1, 1, LengthEdge, WeightEdge, true, true, 0); //阵列过孔 RPinhole.FirstRectangularPatternParameters = CatRectangularPatternParameters.catInstancesandSpacing; RPinhole.SecondRectangularPatternParameters = CatRectangularPatternParameters.catInstancesandSpacing; Spart.Update(); } if (AbortHole) { CreateThreadHole(Spart, UserSelected, UserSelectedValue, WeightEdge, LengthEdge, Weight, Length, JianDis); } }
/// <summary> /// 创建螺纹孔安装面 /// </summary> /// <param name="Spart">对象零件</param> /// <param name="UserSelected">用户选择参考对象2边1面</param> /// <param name="WeightEdge">宽边</param> /// <param name="LengthEdge">长边</param> /// <param name="Weight">宽边宽度</param> /// <param name="Length">长边长度</param> /// <param name="JianDis">孔间距</param> public void CreateThreadHole(Part Spart, Reference[] UserSelected, object[] UserSelectedValue, Edge WeightEdge, Edge LengthEdge, double Weight, double Length, double JianDis) { //对与孔装配的零件执行打孔 object[] SHolePoint = new object[3]; Selection SelectArc = CatActiveDoc.Selection; SelectArc.Clear(); var Result = SelectArc.SelectElement2(InputObjectType(2), "请选择子零件孔支持面!", true); if (Result == "Cancel" || SelectArc == null || SelectArc.Count2 == 0) { return; } Reference SonReface = SelectArc.Item(1).Reference; GetProductByFace SGPB = new GetProductByFace(); Part SonPart = SGPB.GetPart((Face)SelectArc.Item(1).Value); SelectArc.Item(1).GetCoordinates(SHolePoint); SelectArc.Clear(); ShapeFactory NextshapeFactory = (ShapeFactory)SonPart.ShapeFactory; HybridShapeFactory NextHyShapeFactory = (HybridShapeFactory)SonPart.HybridShapeFactory; InstanceFactory NextInf = (InstanceFactory)SonPart.GetCustomerFactory("InstanceFactory"); NextInf.AddInstance(WeightEdge); try { Reference Wref = null; Reference Lref = null; try { object[] LinePoint = new object[12]; object[] LineDirection = new object[12]; SPAWorkbench TheSPAWorkbench = (SPAWorkbench)CatActiveDoc.GetWorkbench("SPAWorkbench"); Measurable LengthA = TheSPAWorkbench.GetMeasurable(WeightEdge); Measurable LengthB = TheSPAWorkbench.GetMeasurable(LengthEdge); //LengthA.GetMinimumDistancePoints(, LinePoint); //LengthA.GetDirection(LineDirection); //NextHyShapeFactory.AddNewLineNormal(SonReface, 10, 10, true); Edge WeightEdgeT = (Edge)UserSelectedValue[0]; Edge LengthEdgeT = (Edge)UserSelectedValue[1]; string WeightEdgeName = WeightEdgeT.get_Name(); string LengthEdgeTName = LengthEdgeT.get_Name(); Wref = SonPart.CreateReferenceFromName(null); Lref = SonPart.CreateReferenceFromName(null); WeightEdgeName = Wref.get_Name(); LengthEdgeTName = Lref.get_Name(); SelectArc.Add(WeightEdgeT); SelectArc.Add(LengthEdgeT); SelectArc.Copy(); } catch (Exception e) { throw e; } Hole SonNwhole = NextshapeFactory.AddNewHoleWith2Constraints(Convert.ToDouble(SHolePoint[0]), Convert.ToDouble(SHolePoint[1]), Convert.ToDouble(SHolePoint[2]), Wref, Lref, SonReface, 50);//创建过孔 try { RxSetHoleType(SonNwhole, 10, Weight / 2, 9, RxHoleType.NSmoothHole); } catch (Exception) { RxSetHoleType(SonPart, SonNwhole, 10, Weight / 2, 9, RxHoleType.NSmoothHole, WeightEdge, LengthEdge); } SonPart.Update(); Hole SonNwPinHole = NextshapeFactory.AddNewHoleWith2Constraints(Convert.ToDouble(SHolePoint[0]) + 5, Convert.ToDouble(SHolePoint[1]) + 5, Convert.ToDouble(SHolePoint[2]), UserSelected[0], UserSelected[1], SonReface, 50);//创建销孔 RxSetHoleType(SonNwPinHole, 10, Weight / 2 + JianDis, 8, RxHoleType.PinHole); SonPart.Update(); if (Length <= 60) { RectPattern RPhole = NextshapeFactory.AddNewRectPattern(SonNwhole, 2, 1, JianDis * 2, 20, 1, 1, LengthEdge, WeightEdge, true, true, 0); //阵列过孔 RPhole.FirstRectangularPatternParameters = CatRectangularPatternParameters.catInstancesandSpacing; RPhole.SecondRectangularPatternParameters = CatRectangularPatternParameters.catInstancesandSpacing; Spart.Update(); } else { RectPattern RPhole = NextshapeFactory.AddNewRectPattern(SonNwhole, 2, 1, JianDis * 2, 20, 1, 1, LengthEdge, WeightEdge, true, true, 0); //阵列过孔 RPhole.FirstRectangularPatternParameters = CatRectangularPatternParameters.catInstancesandSpacing; RPhole.SecondRectangularPatternParameters = CatRectangularPatternParameters.catInstancesandSpacing; RectPattern RPinhole = NextshapeFactory.AddNewRectPattern(SonNwPinHole, 2, 1, JianDis * 2, 20, 1, 1, LengthEdge, WeightEdge, true, true, 0); //阵列过孔 RPinhole.FirstRectangularPatternParameters = CatRectangularPatternParameters.catInstancesandSpacing; RPinhole.SecondRectangularPatternParameters = CatRectangularPatternParameters.catInstancesandSpacing; Spart.Update(); } } catch (Exception) { throw; MessageBox.Show("打孔失败!请确定选择是否有误!"); } }