/// <summary> /// 递归创建装配树 /// </summary> /// <param name="component"></param> /// <param name="swComponet"></param> private static void CreateTree(Component2 component, ref SWComponent swComponent) { if (component == null) { return; } /////////复制组件相关信息/////////// //名称 swComponent.Name = component.Name2; //是否根组件 swComponent.IsRoot = component.IsRoot(); //提取配合,存放在RootComponent节点 //if (swComponent.IsRoot) { swComponent.Mates = GetSWMatesOfRootComponent(component); //} //包围盒 double[] box = null; try { box = component.GetBox(false, false); } catch (Exception e) { //发生异常,说明该组件(根组件)没有包围盒 box = null; } if (box == null) { swComponent.BoundingBox = CreateSWBoundingBox(box); } else { swComponent.BoundingBox = new SWBoundingBox(); } //提取Body object bodyInfo; object[] bodies = component.GetBodies3((int)swBodyType_e.swSolidBody, out bodyInfo); if (bodies != null) { foreach (object objBody in bodies) { swComponent.Bodies.Add(CreateSWBody((Body2)objBody)); } } //继续遍历 object[] subComponents = component.GetChildren(); if (subComponents != null) { foreach (object obj in subComponents) { SWComponent newComponent = new SWComponent(); CreateTree((Component2)obj, ref newComponent); swComponent.SubComponents.Add(newComponent); } } }
private bool SetDecorsForEachDetail(Component2 component, string pathPictName, int number, bool fit = false) { if (component.IGetModelDoc().GetType() == (int)swDocumentTypes_e.swDocPART) { double angel = GetAngel(number, component) ? 0 : 90; var m = component.IGetModelDoc(); var rmArr = (object[])m.Extension.GetRenderMaterials(); RenderMaterial rm = null; object swEnt; string fileName = pathPictName.Substring(0, pathPictName.Length - 4) + ".p2m"; if (rmArr == null) { component.Select(false); swEnt = (Entity)_swModel.ISelectionManager.GetSelectedObject6(1, -1); _swModel.ClearSelection(); //rm = m.Extension.CreateRenderMaterial(fileName); } else { if (_swSelModel.GetCustomInfoValue("", "ExtFanerFeats") == "Yes") swEnt = m; else { rm = (RenderMaterial)rmArr[0]; var ent = (object[])rm.GetEntities(); swEnt = ent[0]; } } rm = m.Extension.CreateRenderMaterial(fileName); rm.AddEntity(swEnt); rm.RotationAngle = angel; rm.TextureFilename = pathPictName; rm.FileName = fileName; rm.FixedAspectRatio = false; rm.FitWidth = false; rm.FitHeight = false; rm.Width = 0.3; rm.Height = 0.3; if (fit) { double width = 1; double height = 1; object b = component.GetBox(true, true); if (b != null) { var boxs = (double[])b; double xs1 = boxs[0]; double ys1 = boxs[1]; double zs1 = boxs[2]; double xs2 = boxs[3]; double ys2 = boxs[4]; double zs2 = boxs[5]; double x = Math.Abs(xs1 - xs2); double z = Math.Abs(zs1 - zs2); double y = Math.Abs(ys1 - ys2); if (y < x && y < z) { width = z; height = x; } else if (x < z && x < y) { width = y; height = z; } else if (z < x && z < y) { width = y; height = x; } } rm.Width = width; rm.Height = height; rm.FitWidth = true; rm.FitHeight = true; } var swConfig = (Configuration)m.GetConfigurationByName(component.ReferencedConfiguration); object displayStateNames = swConfig.GetDisplayStates(); int e1, e2; int f; //var texture = m.Extension.CreateTexture(pathPictName, 1, 0, false); //component.RemoveTexture(string.Empty); //component.SetTexture(string.Empty, texture); m.Extension.AddRenderMaterial(rm, out f); m.Extension.AddDisplayStateSpecificRenderMaterial(rm, (int)swDisplayStateOpts_e.swSpecifyDisplayState, displayStateNames, out e1, out e2); if (fit) m.Save2(true); return true; } return false; }
private bool CheckComponentsIntersection2(Component2 cut, Feature feat, Component2 parentControl) { object boxb1 = cut.GetBox(true, true); object boxb2 = new object(); object boxb3 = parentControl.GetBox(true, true); //int count=0; //var faces = feat.GetFaces(); //double[] faceBox = null; //foreach (var f in faces) //{ // faceBox = (((Face)(f)).GetBody() as Body).GetBodyBox(); // //if ((boxb2 as double[]) == null) // // boxb2 = new double[6]; //} //if (faceBox != null) // boxb2 = faceBox; //else feat.GetBox(ref boxb2); if ((boxb2 as double[]) != null && boxb1 != null && boxb3 != null) { var box1 = (double[])boxb1; double xs1 = box1[0]; double ys1 = box1[1]; double zs1 = box1[2]; double xs2 = box1[3]; double ys2 = box1[4]; double zs2 = box1[5]; var box3 = (double[])boxb3; var box2 = (double[])boxb2; double xf1 = box2[0]; double yf1 = box2[1]; double zf1 = box2[2] + box3[2]; double xf2 = box2[3]; double yf2 = box2[4]; double zf2 = box2[5] + box3[5]; if ((((xf1 > xs1 && xf1 < xs2) || (xf1 > xs2 && xf1 < xs1)) && ((yf1 > ys1 && yf1 < ys2) || (yf1 > ys2 && yf1 < ys1)) && ((zf1 > zs1 && zf1 < zs2) || (zf1 > zs2 && zf1 < zs1))) || (((xf2 > xs1 && xf2 < xs2) || (xf2 > xs2 && xf2 < xs1)) && ((yf2 > ys1 && yf2 < ys2) || (yf2 > ys2 && yf2 < ys1)) && ((zf2 > zs1 && zf2 < zs2) || (zf2 > zs2 && zf2 < zs1))) || (((xf2 > xs1 && xf2 < xs2) || (xf2 > xs2 && xf2 < xs1)) && ((yf1 > ys1 && yf1 < ys2) || (yf1 > ys2 && yf1 < ys1)) && ((zf1 > zs1 && zf1 < zs2) || (zf1 > zs2 && zf1 < zs1))) || (((xf2 > xs1 && xf2 < xs2) || (xf2 > xs2 && xf2 < xs1)) && ((yf1 > ys1 && yf1 < ys2) || (yf1 > ys2 && yf1 < ys1)) && ((zf2 > zs1 && zf2 < zs2) || (zf2 > zs2 && zf2 < zs1))) || (((xf2 > xs1 && xf2 < xs2) || (xf2 > xs2 && xf2 < xs1)) && ((yf2 > ys1 && yf2 < ys2) || (yf2 > ys2 && yf2 < ys1)) && ((zf1 > zs1 && zf1 < zs2) || (zf1 > zs2 && zf1 < zs1))) || (((xf1 > xs1 && xf1 < xs2) || (xf1 > xs2 && xf1 < xs1)) && ((yf2 > ys1 && yf2 < ys2) || (yf2 > ys2 && yf2 < ys1)) && ((zf1 > zs1 && zf1 < zs2) || (zf1 > zs2 && zf1 < zs1))) || (((xf1 > xs1 && xf1 < xs2) || (xf1 > xs2 && xf1 < xs1)) && ((yf1 > ys1 && yf1 < ys2) || (yf1 > ys2 && yf1 < ys1)) && ((zf2 > zs1 && zf2 < zs2) || (zf2 > zs2 && zf2 < zs1))) || (((xf1 > xs1 && xf1 < xs2) || (xf1 > xs2 && xf1 < xs1)) && ((yf2 > ys1 && yf2 < ys2) || (yf2 > ys2 && yf2 < ys1)) && ((zf2 > zs1 && zf2 < zs2) || (zf2 > zs2 && zf2 < zs1))) ) return true; else return false; } return false; }
private bool CheckComponentsIntersection(Component2 component1, Component2 component2) { object b = component1.GetBox(true, true); if (b != null) { var boxs = (double[])b; double xs1 = boxs[0]; double ys1 = boxs[1]; double zs1 = boxs[2]; double xs2 = boxs[3]; double ys2 = boxs[4]; double zs2 = boxs[5]; b = component2.GetBox(true, true); if (b != null) { var boxf = (double[])b; double xf1 = boxf[0]; double yf1 = boxf[1]; double zf1 = boxf[2]; double xf2 = boxf[3]; double yf2 = boxf[4]; double zf2 = boxf[5]; if (((xf1 >= xs1 - 0.00000001 && xf1 - 0.00000001 <= xs2) || (xs1 >= xf1 - 0.00000001 && xs1 - 0.00000001 <= xf2)) && ((yf1 >= ys1 - 0.00000001 && yf1 - 0.00000001 <= ys2) || (ys1 >= yf1 - 0.00000001 && ys1 - 0.00000001 <= yf2)) && ((zf1 >= zs1 - 0.00000001 && zf1 - 0.00000001 <= zs2) || (zs1 >= zf1 - 0.00000001 && zs1 - 0.00000001 <= zf2))) return true; else return false; } } return false; }
/// <summary> /// 递归创建装配树 /// </summary> /// <param name="component"></param> /// <param name="swComponet"></param> private static void CreateTree(Component2 component, ref SWComponent swComponent) { if (component == null) { return; } /////////复制组件相关信息/////////// //名称 swComponent.Name = component.Name2; //是否根组件 swComponent.IsRoot = component.IsRoot(); //提取配合,存放在RootComponent节点 //if (swComponent.IsRoot) { swComponent.Mates = GetSWMatesOfRootComponent(component); //} //包围盒 double[] box = null; try { box = component.GetBox(false, false); } catch (Exception e) { //发生异常,说明该组件(根组件)没有包围盒 box = null; } if (box == null) { swComponent.BoundingBox = CreateSWBoundingBox(box); } else { swComponent.BoundingBox = new SWBoundingBox(); } //提取Body object bodyInfo; object[] bodies = component.GetBodies3((int)swBodyType_e.swSolidBody, out bodyInfo); if (bodies != null) { foreach (object objBody in bodies) { swComponent.Bodies.Add(CreateSWBody((Body2)objBody)); } } //继续遍历 object[] subComponents = component.GetChildren(); if (subComponents != null) { foreach (object obj in subComponents) { SWComponent newComponent = new SWComponent(); CreateTree((Component2)obj, ref newComponent); swComponent.SubComponents.Add(newComponent); } } }
public void TableTopProcess(Component2 swAddedComp) { var mates = swAddedComp.GetMates(); Component2 firstComponent = null; var swAddedCompModel = (ModelDoc2)swAddedComp.GetModelDoc(); bool? isLeft = null; if (swAddedCompModel.GetCustomInfoValue("", "KitchenType").Contains("левая")) isLeft = true; else if (swAddedCompModel.GetCustomInfoValue("", "KitchenType").Contains("правая")) isLeft = false; if (isLeft == null) return; if (mates != null) { foreach (var mate in mates) { if (mate is Mate2) { var spec = (Mate2)mate; int mec = spec.GetMateEntityCount(); if (mec > 1) { for (int ik = 0; ik < mec; ik++) { MateEntity2 me = spec.MateEntity(ik); if (me.ReferenceComponent.Name.Contains(swAddedComp.Name)) { string firstComp = spec.MateEntity(0).ReferenceComponent.Name.Split('/')[0]; swAddin.GetComponentByName(RootModel, firstComp, false, out firstComponent); break; } } } } } } if (firstComponent != null) { //привязать... swAddin.AddMate(RootModel, firstComponent.FeatureByName("#swrfЗадняя"), swAddedComp.FeatureByName("#swrfЗадняя"), true); } bool status; var swComponents = new LinkedList<Component2>(); if (swAddin.GetComponents(swRootComponent, swComponents, false, false)) { double[] origBox = swAddedComp.GetBox(true, true); double origaverx = Math.Min(origBox[3], origBox[0]) + Math.Abs(origBox[3] - origBox[0]) / 2; double origaverz = Math.Min(origBox[5], origBox[2]) + Math.Abs(origBox[5] - origBox[2]) / 2; var swCompModel = (ModelDoc2)firstComponent.GetModelDoc(); bool isAnglePartFirst = false, isUpPartfirst = false, isTabletopFirst = false; if (swCompModel != null) GetTypeProperty(swCompModel.GetCustomInfoValue("", "KitchenType"), out isAnglePartFirst, out isUpPartfirst, out isTabletopFirst); if (isTabletopFirst || isUpPartfirst) return; string tmp = (bool)isLeft ? "#swrfЛевая" : "#swrfПравая"; if (isAnglePartFirst) swAddin.AddMate(RootModel, firstComponent.FeatureByName("#swrfЗадняя2"), swAddedComp.FeatureByName(tmp), true); else swAddin.AddMate(RootModel, firstComponent.FeatureByName(tmp), swAddedComp.FeatureByName(tmp), true); InfoForMate maxDistance = FindMinTopTable(swComponents, swAddedComp, isLeft); InfoForMate maxDistance3 = FindMaxPlate(swComponents, swAddedComp, firstComponent, origaverx, origaverz, isAnglePartFirst, isLeft); if (maxDistance.planeDist != null && maxDistance.planeSource != null && maxDistance3.distance > maxDistance.distance)//&& maxDistance3.planeSource.Name == maxDistance.planeSource.Name) { swModel.ClearSelection(); InfoForMate maxDistance2 = new InfoForMate(double.MinValue, null, null); string tt = isAnglePartFirst ? "#swrfЗадняя2" : plateleft; status = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", tt, firstComponent.Name, rootName), "PLANE", 0, 0, 0, false, 0, null, 0); maxDistance.planeDist.Select(true); measure.Calculate(null); if (measure.IsParallel && maxDistance2.distance < measure.Distance) maxDistance2 = new InfoForMate(measure.Distance, firstComponent.FeatureByName(tt), null); swModel.ClearSelection(); status = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", plateright, firstComponent.Name, rootName), "PLANE", 0, 0, 0, false, 0, null, 0); maxDistance.planeDist.Select(true); measure.Calculate(null); if (measure.IsParallel && maxDistance2.distance < measure.Distance) maxDistance2 = new InfoForMate(measure.Distance, firstComponent.FeatureByName(plateright), null); //if (maxDistance2.planeDist!=null) // swAddin.AddMate(swModel, maxDistance2.planeDist, maxDistance.planeSource, true);//distToTopTable = new InfoForMate(maxDistance.distance, maxDistance2.planeDist, maxDistance.planeSource);// swModel.ClearSelection(); maxDistance.planeDist.Select(false); maxDistance2.planeDist.Select(true); measure.Calculate(null); maxDistance.distance = measure.Distance; } else { maxDistance = maxDistance3;//maxDistance = FindMaxPlate(swComponents, swAddedComp, firstComponent, origaverx, origaverz,isAnglePartFirst); //if (maxDistance.planeDist != null && maxDistance.planeSource != null) //{ // if (maxDistance.planeSource.Name != "#swrfЗадняя2") // swAddin.AddMate(swModel, maxDistance.planeSource, swAddedComp.FeatureByName(maxDistance.planeSource.Name), true); // else // { // maxDistance.planeSource.Select(false); // swAddedComp.FeatureByName(plateleft).Select(true); // measure.Calculate(null); // double distanceleft = measure.Distance; // maxDistance.planeSource.Select(false); // swAddedComp.FeatureByName(plateright).Select(true); // measure.Calculate(null); // double distanceright = measure.Distance; // if (distanceleft<distanceright) // swAddin.AddMate(swModel, maxDistance.planeSource, swAddedComp.FeatureByName(plateleft), true); // else // swAddin.AddMate(swModel, maxDistance.planeSource, swAddedComp.FeatureByName(plateright), true); // } //} } double distance2; swModel.ClearSelection(); if (!isAnglePartFirst) status = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", "Передняя", firstComponent.Name, rootName), "PLANE", 0, 0, 0, false, 0, null, 0); else status = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", plateleft, firstComponent.Name, rootName), "PLANE", 0, 0, 0, false, 0, null, 0); status = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", "#swrfЗадняя", firstComponent.Name, rootName), "PLANE", 0, 0, 0, true, 0, null, 0); measure.Calculate(null); distance2 = measure.Distance * 1000; //поменять размер.. var curModel = swAddedComp.GetModelDoc2(); bool isNumber = false; OleDbConnection oleDb; OleDbDataReader rd; List<string> strObjNames = new List<string>(); string filePath = swAddedComp.GetPathName(); if (swAddin.OpenModelDatabase(curModel, out oleDb)) { using (oleDb) { OleDbCommand cm; cm = isNumber ? new OleDbCommand( "SELECT * FROM objects WHERE number>0 ORDER BY number", oleDb) : new OleDbCommand("SELECT * FROM objects ORDER BY id", oleDb); rd = cm.ExecuteReader(); while (rd.Read()) { if (rd["caption"].ToString() == null || rd["caption"].ToString() == "" || rd["caption"].ToString().Trim() == "") continue; string strObjName = rd["name"].ToString(); if (filePath.Contains("_SWLIB_BACKUP")) { string pn = Path.GetFileNameWithoutExtension(filePath); string last3 = pn.Substring(pn.Length - 4, 4); string[] arr = strObjName.Split('@'); if (arr.Length != 3) throw new Exception("что-то не так"); arr[2] = Path.GetFileNameWithoutExtension(arr[2]) + last3 + Path.GetExtension(arr[2]); strObjName = string.Format("{0}@{1}@{2}", arr[0], arr[1], arr[2]); } strObjNames.Add(strObjName); } } } swAddin.SetObjectValue(curModel, strObjNames[0], 14, maxDistance.distance * 1000); swAddin.SetObjectValue(curModel, strObjNames[1], 14, distance2); } }
public InfoForMate GetSimilarTables(Component2 swComp2, bool isUpPart, bool isAnglePartOrig) { var swComponents = new LinkedList<Component2>(); InfoForMate minDistance = new InfoForMate(double.MaxValue, null, null); string origplateright = plateright; string origplateleft = plateleft; if (isAnglePartOrig) { origplateright = "#swrfПраваяЗадняя"; origplateleft = "#swrfЛеваяЗадняя"; } if (swAddin.GetComponents(swRootComponent, swComponents, false, false)) { double[] origBox = swComp2.GetBox(true, true); double origaverx = Math.Min(origBox[3], origBox[0]) + Math.Abs(origBox[3] - origBox[0]) / 2; double origaverz = Math.Min(origBox[5], origBox[2]) + Math.Abs(origBox[5] - origBox[2]) / 2; foreach (Component2 component in swComponents) { bool isAnglePart, isDistUpPart, isTabletop, isKtExist; var swCompModel = (ModelDoc2)component.GetModelDoc(); if (swCompModel == null) continue; isKtExist = GetTypeProperty(swCompModel.GetCustomInfoValue("", "KitchenType"), out isAnglePart, out isDistUpPart, out isTabletop); if (!isKtExist) continue; if (isTabletop) continue; if (isDistUpPart != isUpPart) continue; double[] currentBox = component.GetBox(true, true); double averx = Math.Min(currentBox[3], currentBox[0]) + Math.Abs(currentBox[3] - currentBox[0]) / 2; double averz = Math.Min(currentBox[5], currentBox[2]) + Math.Abs(currentBox[5] - currentBox[2]) / 2; double a = Math.Abs(averx - origaverx); double b = Math.Abs(averz - origaverz); double c = Math.Sqrt(a * a + b * b) * 1000; if (c > 4000) continue; string compName = component.Name2; if (compName.Contains("Замер") || compName == swComp2.Name) continue; bool bb = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", "#swrfЗадняя", compName, rootName), "PLANE", 0, 0, 0, false, 0, null, 0); bb = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", "#swrfЗадняя", swComp2.Name, rootName), "PLANE", 0, 0, 0, true, 0, null, 0); measure.Calculate(null); if (!(measure.IsIntersect && measure.IsParallel)) { if (isAnglePart) { bb = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", "#swrfЗадняя2", compName, rootName), "PLANE", 0, 0, 0, false, 0, null, 0); bb = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", "#swrfЗадняя", swComp2.Name, rootName), "PLANE", 0, 0, 0, true, 0, null, 0); measure.Calculate(null); if (!(measure.IsIntersect && measure.IsParallel)) continue; } else continue; } swModel.ClearSelection(); bb = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", plateright, compName, rootName), "PLANE", 0, 0, 0, false, 0, null, 0); bb = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", origplateleft, swComp2.Name, rootName), "PLANE", 0, 0, 0, true, 0, null, 0); measure.Calculate(null); if (!measure.IsParallel && !isAnglePart) continue; if (minDistance.distance > measure.Distance && measure.IsParallel) { minDistance = new InfoForMate(measure.Distance, component.FeatureByName(plateright), swComp2.FeatureByName(origplateleft)); if (isAnglePart) { swModel.ClearSelection(); bb = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", plateright, compName, rootName), "PLANE", 0, 0, 0, false, 0, null, 0); bb = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", origplateright, swComp2.Name, rootName), "PLANE", 0, 0, 0, true, 0, null, 0); measure.Calculate(null); if (minDistance.distance > measure.Distance && measure.IsParallel) { minDistance = new InfoForMate(measure.Distance, component.FeatureByName(plateright), swComp2.FeatureByName(origplateright)); } } } bb = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", plateleft, compName, rootName), "PLANE", 0, 0, 0, false, 0, null, 0); bb = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", origplateright, swComp2.Name, rootName), "PLANE", 0, 0, 0, true, 0, null, 0); measure.Calculate(null); if (!measure.IsParallel) continue; if (minDistance.distance > measure.Distance) { minDistance = new InfoForMate(measure.Distance, component.FeatureByName(plateleft), swComp2.FeatureByName(origplateright)); if (isAnglePart) { swModel.ClearSelection(); bb = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", plateleft, compName, rootName), "PLANE", 0, 0, 0, false, 0, null, 0); bb = RootModel.Extension.SelectByID2(string.Format("{0}@{1}@{2}", origplateleft, swComp2.Name, rootName), "PLANE", 0, 0, 0, true, 0, null, 0); measure.Calculate(null); if (minDistance.distance > measure.Distance && measure.IsParallel) { minDistance = new InfoForMate(measure.Distance, component.FeatureByName(plateleft), swComp2.FeatureByName(origplateleft)); } } } } } return minDistance; }