/// <summary> /// 提取装备文件拓扑结构 /// </summary> public static void ExtractTree() { ModelDoc2 swModel = default(ModelDoc2); Component2 rootComponent = default(Component2); SWComponent swComponent = default(SWComponent); swModel = swApp.ActiveDoc; rootComponent = swModel.ConfigurationManager.ActiveConfiguration.GetRootComponent3(true); swComponent = new SWComponent(); CreateTree(rootComponent, ref swComponent); //序列化 XmlSerializer xmlSerializer = new XmlSerializer(typeof(SWComponent)); StreamWriter file = new StreamWriter(@"" + swComponent.Name + ".xml"); xmlSerializer.Serialize(file, swComponent); file.Close(); }
/// <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); } } }