/// <summary> /// 编辑零部件,自动区分装配体还是零件 /// </summary> /// <param name="comp">组件</param> /// <param name="doc">顶级文档</param> /// <param name="silent">静默编辑--提高速度</param> /// <param name="readOnly">只读</param> /// <returns></returns> public static swEditPartCommandStatus_e Edit(this IComponent2 comp, IAssemblyDoc doc, bool silent = true, bool readOnly = false) { var type = comp.GetCompType(); int info = -1; switch (type) { case swDocumentTypes_e.swDocPART: if (comp.Select2(false, -1)) { doc.EditPart2(silent, readOnly, ref info); } break; case swDocumentTypes_e.swDocASSEMBLY: if (comp.Select2(false, -1)) { doc.EditAssembly(); } info = 0; break; default: throw new FileFormatException(string.Format("can not edit component width type of:{0} ", type.ToString())); } swEditPartCommandStatus_e state = (swEditPartCommandStatus_e)info; return(state); }
public static Loader Initialize(ISldWorks solidWorks, IAssemblyDoc assembly) { if (Instance == null) { Instance = new Loader(solidWorks, assembly); } return Instance; }
/// <summary> /// Obtains the components. /// </summary> /// <param name="swAssy">The SolidWorks assembly.</param> /// <returns>All child components.</returns> public static List <IComponent2> ObtainChildComponents(IAssemblyDoc swAssy) { // Obtain the root component var swModel = (IModelDoc2)swAssy; IConfiguration swConfiguration = swModel.GetActiveConfiguration(); IComponent2 swRootComp = swConfiguration.GetRootComponent3(true); // Build a list of all child components return(GetChildren(swRootComp)); }
/// <summary> /// 插入新零件 /// </summary> /// <typeparam name="TFaceOrRefplane"></typeparam> /// <param name="doc"><see cref="IAssemblyDoc"/> Interface</param> /// <param name="filePath"> 文件路径</param> /// <param name="FaceOrPlane"> 安装的面</param> /// <param name="throwError">遇到错误是否抛出异常</param> public static swInsertNewPartErrorCode_e InsertNewPart2Ex <TFaceOrRefplane>(this IAssemblyDoc doc, string filePath, TFaceOrRefplane FaceOrPlane, bool throwError = true) { var result = doc.InsertNewPart2(filePath, FaceOrPlane).CastObj <swInsertNewPartErrorCode_e>(); if (throwError && result != swInsertNewPartErrorCode_e.swInsertNewPartError_NoError) { throw new InvalidCastException($"Error:{result.ToString()}"); } return(result); }
/// <summary> /// Extension for <see cref="IAssemblyDoc.AddMate5(int, int, bool, double, double, double, double, double, double, double, double, bool, bool, int, out int)"/> /// </summary> /// <param name="doc"><see cref="IAssemblyDoc"/></param> /// <param name="mateType"><see cref="swMateType_e"/>配合类型</param> /// <param name="mateAlign"><see cref="MateAlignExtension"/>同向或者反向配合</param> /// <param name="flip">是否反转配合,只在距离配合时起作用</param> /// <param name="distance">距离参数 参数顺序:距离 最大距离值 最小距离值</param> /// <param name="gearRatioParam">齿轮配合参数 参数顺序:GearRatioNumerator GearRatioDenominator </param> /// <param name="angle">角度参数 顺序: 角度 角度最大绝对值 角度最小绝对值</param> /// <param name="forPostionOnly">仅使用位置</param> /// <param name="lockRotation">锁定位置</param> /// <param name="widthMateOption">宽度配合参数</param> /// <param name="error">错误</param> /// <param name="throwIfError">是否在错误时抛出异常,默认为抛出</param> /// <returns></returns> public static IMate2 AddMate5Ex(this IAssemblyDoc doc, swMateType_e mateType, MateAlignExtension mateAlign, bool flip, Tuple <double, double, double> distance, Tuple <double, double> gearRatioParam, Tuple <double, double, double> angle, bool forPostionOnly, bool lockRotation, swMateWidthOptions_e widthMateOption, out swAddMateError_e error, bool throwIfError = true) { int error_int; var mate = doc.AddMate5(mateType.SWToInt(), mateAlign == MateAlignExtension.SameAlign ? swMateAlign_e.swMateAlignALIGNED.SWToInt() : swMateAlign_e.swMateAlignANTI_ALIGNED.SWToInt(), flip, distance.Item1, distance.Item2, distance.Item3, gearRatioParam.Item1, gearRatioParam.Item2, angle.Item1, angle.Item2, angle.Item3, forPostionOnly, lockRotation, widthMateOption.SWToInt(), out error_int); error = error_int.CastObj <swAddMateError_e>(); if (throwIfError && error != swAddMateError_e.swAddMateError_NoError) { throw new InvalidOperationException($"Mate Error:{error.ToString()}"); } return(mate); }
public int IndexFaces(IAssemblyDoc assm) { var count = 0; var start = DateTime.Now; { var comps = assm.GetComponents(false) as object[]; if (comps != null) { foreach (IComponent2 comp in comps) { object bodyInfo; var bodies = comp.GetBodies3((int)swBodyType_e.swAllBodies, out bodyInfo) as object[]; if (bodies != null) { foreach (IBody2 body in bodies) { var faces = body.GetFaces() as object[]; if (faces != null) { foreach (IFace2 face in faces) { var surf = face.IGetSurface(); var type = (swSurfaceTypes_e)surf.Identity(); count++; Trace.WriteLine($"Area: {face.GetArea()}. Type: {type}"); } } } } } } } App.SendMsgToUser($"{count} face(s) of {(assm as IModelDoc2).GetTitle()} indexed in {DateTime.Now.Subtract(start).TotalSeconds} seconds"); return(count); }
/// <summary> /// 面重合配合 /// </summary> /// <typeparam name="TFaceOne"></typeparam> /// <typeparam name="TFaceTwo"></typeparam> /// <param name="doc"></param> /// <param name="faceOne"></param> /// <param name="faceTwo"></param> /// <param name="mateAlign"></param> /// <returns></returns> public static Mate2 AddFaceCoinMate <TFaceOne, TFaceTwo>(this IAssemblyDoc doc, TFaceOne faceOne, TFaceTwo faceTwo, MateAlignExtension mateAlign) { int mateError = 0; Mate2 swMate = default; SelectEntityOrFeature(faceOne, false); SelectEntityOrFeature(faceTwo, true); switch (mateAlign) { case MateAlignExtension.SameAlign: swMate = doc.AddMate5((int)swMateType_e.swMateCOINCIDENT, (int)swMateAlign_e.swMateAlignALIGNED, false, 0, 0, 0, 0, 0, 0, 0, 0, false, false, 0, out mateError); break; case MateAlignExtension.AntiAlign: swMate = doc.AddMate5((int)swMateType_e.swMateCOINCIDENT, (int)swMateAlign_e.swMateAlignANTI_ALIGNED, false, 0, 0, 0, 0, 0, 0, 0, 0, false, false, 0, out mateError); break; default: break; } return(swMate); }
/// <summary> /// 面重合配合 /// </summary> /// <typeparam name="TFaceOne"></typeparam> /// <typeparam name="TFaceTwo"></typeparam> /// <param name="doc"></param> /// <param name="faceOne"></param> /// <param name="faceTwo"></param> /// <param name="mateAlign"></param> /// <param name="dis">距离,正负代表方向</param> /// <returns></returns> public static Mate2 AddFaceDisMate <TFaceOne, TFaceTwo>(this IAssemblyDoc doc, TFaceOne faceOne, TFaceTwo faceTwo, MateAlignExtension mateAlign, double dis) { int mateError = 0; Mate2 swMate = default; SelectEntityOrFeature(faceOne, false); SelectEntityOrFeature(faceTwo, true); switch (mateAlign) { case MateAlignExtension.SameAlign: if (dis > 0) { swMate = doc.AddMate5((int)swMateType_e.swMateDISTANCE, (int)swMateAlign_e.swMateAlignALIGNED, false, dis, dis, dis, 0, 0, 0, 0, 0, false, false, 0, out mateError); } else { swMate = doc.AddMate5((int)swMateType_e.swMateDISTANCE, (int)swMateAlign_e.swMateAlignALIGNED, true, -dis, -dis, -dis, 0, 0, 0, 0, 0, false, false, 0, out mateError); } break; case MateAlignExtension.AntiAlign: if (dis > 0) { swMate = doc.AddMate5((int)swMateType_e.swMateDISTANCE, (int)swMateAlign_e.swMateAlignANTI_ALIGNED, false, dis, dis, dis, 0, 0, 0, 0, 0, false, false, 0, out mateError); } else { swMate = doc.AddMate5((int)swMateType_e.swMateDISTANCE, (int)swMateAlign_e.swMateAlignANTI_ALIGNED, true, -dis, -dis, -dis, 0, 0, 0, 0, 0, false, false, 0, out mateError); } break; default: break; } return(swMate); }
public void BeginIndexFaces(IAssemblyDoc assm, IFaceIndexerCallback callback) { m_ProcessingQueue.Add(new KeyValuePair <IAssemblyDoc, IFaceIndexerCallback>(assm, callback)); }
public Assembly(IAssemblyDoc swAssembly, IFeature relatedFeature) : base(relatedFeature) { this.swAssembly = swAssembly; Initialize(); }
public Assembly(IAssemblyDoc swAssembly) : this(swAssembly, null) { }
internal SwAssembly(IAssemblyDoc assembly, ISldWorks app, ILogger logger) : base((IModelDoc2)assembly, app, logger) { Assembly = assembly; }
public void IndexFacesCompleted(IAssemblyDoc assm, int count) { Console.WriteLine($"Indexing completed in '{(assm as IModelDoc2).GetTitle()}' for {count} face(s) in stand-alone application"); }
private bool Initalize() { helper = Helper.Initialize(solidWorks.IGetMathUtility()); corpus = new Locator(solidWorks.IGetMathUtility()); shaft = new Locator(solidWorks.IGetMathUtility()); planeLocator = new Locator(solidWorks.IGetMathUtility()); prismLocator1 = new Locator(solidWorks.IGetMathUtility()); prismLocator2 = new Locator(solidWorks.IGetMathUtility()); document = solidWorks.ActiveDoc as IModelDoc2; assembly = document as IAssemblyDoc; if (assembly == null) { ShowMessage("Please, open document with assembly"); return false; } horizont = assembly.IFeatureByName(TOP_PLANE_NAME_EN); horizont = horizont == null ? assembly.IFeatureByName(TOP_PLANE_NAME_RU) : horizont; loader = Loader.Initialize(solidWorks, assembly); mounter = Mounter.Initialize(document); Debug.WriteLine("loading of assembly ... done!"); object[] components = assembly.GetComponents(true); foreach(IComponent2 component in components) { if (component.Name2.Contains(configuration[Property.MAIN_DETAIL_NAME])) { shaft.Component = component; Debug.WriteLine("shaft search ... done!"); break; } } if (shaft.Component == null) { ShowMessage("Please, add shaft to assembly or enter other/correct name"); return false; } return true; }
public Loader(ISldWorks solidWorks, IAssemblyDoc assembly) { SolidWorks = solidWorks; Assembly = assembly; }
private void SetExtrudeDepth(IAssemblyDoc assy, IComponent2 comp, string extrudeFeatName, double depth) { if (comp != null) { if (comp.GetSuppression() == (int)swComponentSuppressionState_e.swComponentLightweight || comp.GetSuppression() == (int)swComponentSuppressionState_e.swComponentFullyLightweight) { if (comp.SetSuppression2((int)swComponentSuppressionState_e.swComponentResolved) != (int)swSuppressionError_e.swSuppressionChangeOk) { throw new InvalidOperationException("Failed to set component state to resolved"); } } else if (comp.GetSuppression() == (int)swComponentSuppressionState_e.swComponentSuppressed) { throw new NotSupportedException("Suppressed component is not supported"); } var feat = comp.FeatureByName(extrudeFeatName); if (feat == null) { throw new MissingMemberException("Feature is not found in the component"); } if (comp.Select4(false, null, false)) { int info = -1; if (assy.EditPart2(true, false, ref info) == (int)swEditPartCommandStatus_e.swEditPartSuccessful) { var featData = feat.GetDefinition() as IExtrudeFeatureData2; if (featData != null) { if (featData.AccessSelections(assy, comp)) { featData.SetDepth(true, depth); if (feat.ModifyDefinition(featData, assy, comp)) { (assy as IModelDoc2).ClearSelection2(true); assy.EditAssembly();//Exit edit part mode } else { throw new Exception("Failed to set the depth to the feature"); } } else { throw new InvalidOperationException("Failed to access feature"); } } else { throw new InvalidCastException("Selected feature is not an extrude feature"); } } else { throw new InvalidOperationException("Failed to edit part"); } } else { throw new InvalidOperationException("Failed to select component"); } } else { throw new Exception("Select component"); } }
internal SwAssembly(IAssemblyDoc assembly, SwApplication app, IXLogger logger, bool isCreated) : base((IModelDoc2)assembly, app, logger, isCreated) { m_LazyConfigurations = new Lazy <SwAssemblyConfigurationCollection>(() => new SwAssemblyConfigurationCollection(app.Sw, this)); }
internal SwAssembly(IAssemblyDoc assembly, ISwApplication app, IXLogger logger, bool isCreated) : base((IModelDoc2)assembly, app, logger, isCreated) { Components = new SwAssemblyComponentCollection(this); }