/// <summary> /// Recurses features and sub-features and provides a callback action to process and work with each feature /// </summary> /// <param name="featureAction">The callback action that is called for each feature in the model</param> /// <param name="startFeature">The feature to start at</param> /// <param name="featureDepth">The current depth of the sub-features based on the original calling feature</param> private static void RecurseFeatures(Action <ModelFeature, int> featureAction, Feature startFeature = null, int featureDepth = 0) { // Get the current feature var currentFeature = startFeature; // While that feature is not null... while (currentFeature != null) { // Now get the first sub-feature var subFeature = currentFeature.GetFirstSubFeature() as Feature; // Get the next feature if we should var nextFeature = default(Feature); if (featureDepth == 0) { nextFeature = currentFeature.GetNextFeature() as Feature; } // Create model feature using (var modelFeature = new ModelFeature((Feature)currentFeature)) { // Inform callback of the feature featureAction(modelFeature, featureDepth); } // While we have a sub-feature... while (subFeature != null) { // Get its next sub-feature var nextSubFeature = subFeature.GetNextSubFeature() as Feature; // Recurse all of the sub-features RecurseFeatures(featureAction, subFeature, featureDepth + 1); // And once back up out of the recursive dive // Move to the next sub-feature and process that subFeature = nextSubFeature; } // If we are at the top-level... if (featureDepth == 0) { // And update the current feature reference to the next feature // to carry on the loop currentFeature = nextFeature; } // Otherwise... else { // Remove the current feature as it is a sub-feature // and is processed in the `while (subFeature != null)` loop currentFeature = null; } } }
/// <summary> /// Gets the <see cref="ModelFeature"/> of the item in the feature tree based on its name /// </summary> /// <param name="featureName">Name of the feature</param> /// <returns>The <see cref="ModelFeature"/> for the named feature</returns> public void GetFeatureByName(string featureName, Action <ModelFeature> action) { // Wrap any error SolidDnaErrors.Wrap(() => { // Create feature using (var model = new ModelFeature((Feature)mBaseObject.FeatureByName(featureName))) { // Run action action(model); } }, SolidDnaErrorTypeCode.SolidWorksModel, SolidDnaErrorCode.SolidWorksModelAssemblyGetFeatureByNameError, Localization.GetString(nameof(SolidDnaErrorCode.SolidWorksModelAssemblyGetFeatureByNameError))); }
/// <summary> /// Casts the object to a <see cref="ModelFeature"/> /// Check with <see cref="IsFeature"/> first to assure that it is this type /// </summary> /// <param name="action">The feature is passed into this action to be used within it</param> public void AsFeature(Action <ModelFeature> action) { // Wrap any error SolidDnaErrors.Wrap(() => { // Create feature using (var model = new ModelFeature((Feature)mBaseObject)) { // Run action action(model); } }, SolidDnaErrorTypeCode.SolidWorksModel, SolidDnaErrorCode.SolidWorksModelSelectedObjectCastError, Localization.GetString("SolidWorksModelSelectedObjectCastError")); }
public static ModelFeatureType SpecificFeatureType(this ModelFeature feature) { // Get type name var type = feature?.FeatureTypeName; // Make sure we have one if (feature == null || string.IsNullOrEmpty(type)) { return(ModelFeatureType.None); } // Map to feature types based on this list // http://help.solidworks.com/2016/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IFeature~GetTypeName2.html switch (type) { #region Assembly case "ExplodeLineProfileFeature": return(ModelFeatureType.Sketch); case "InContextFeatHolder": return(ModelFeatureType.Feature); case "MateCoincident": case "MateConcentric": case "MateDistanceDim": case "MateInPlace": case "MateParallel": case "MatePerpendicular": case "MatePlanarAngleDim": case "MateSymmetric": case "MateTangent": case "MateWidth": return(ModelFeatureType.Mate); case "Reference": return(ModelFeatureType.MateReference); case "SmartComponentFeature": return(ModelFeatureType.SmartComponentFeatureData); #endregion #region Body case "APattern": return(ModelFeatureType.FillPatternData); case "BaseBody": return(ModelFeatureType.ExtrudeData); // NOTE: No interface case "Bending": return(ModelFeatureType.Flex); case "Blend": case "BlendCut": return(ModelFeatureType.LoftData); case "Boss": case "BossThin": return(ModelFeatureType.ExtrudeData); case "Chamfer": return(ModelFeatureType.ChamferData); case "CirPattern": return(ModelFeatureType.CircularPatternData); case "CombineBodies": return(ModelFeatureType.CombineBodiesData); case "CosmeticThread": return(ModelFeatureType.CosmeticThreadData); case "CreateAssemFeat": return(ModelFeatureType.SaveBodyData); case "CurvePattern": return(ModelFeatureType.CurveDrivenPatternData); case "Cut": case "CutThin": return(ModelFeatureType.ExtrudeData); // NOTE: No interface case "Deform": return(ModelFeatureType.Deform); case "DeleteBody": return(ModelFeatureType.DeleteBodyData); case "DelFace": return(ModelFeatureType.DeleteFaceData); case "DerivedCirPattern": case "DerivedLPattern": return(ModelFeatureType.DerivedPatternData); case "DimPattern": return(ModelFeatureType.DimPatternData); case "Dome": return(ModelFeatureType.DomeData); case "Draft": return(ModelFeatureType.DraftData); case "EdgeMerge": return(ModelFeatureType.HealEdgesData); case "Emboss": return(ModelFeatureType.WrapSketchData); case "Extrusion": return(ModelFeatureType.ExtrudeData); case "Fillet": return(ModelFeatureType.SimpleFilletData); case "Helix": return(ModelFeatureType.HelixData); case "HoleSeries": return(ModelFeatureType.HoleSeriesData); case "HoleWzd": return(ModelFeatureType.HoleWizardData); // NOTE: No interface case "Imported": return(ModelFeatureType.Imported); case "LocalChainPattern": return(ModelFeatureType.ChainPatternData); case "LocalCirPattern": return(ModelFeatureType.LocalCircularPatternData); case "LocalCurvePattern": return(ModelFeatureType.LocalCurvePatternData); case "LocalLPattern": return(ModelFeatureType.LocalLinearPatternData); case "LPattern": return(ModelFeatureType.LinearPatternData); case "MacroFeature": return(ModelFeatureType.MacroData); case "MirrorPattern": return(ModelFeatureType.MirrorPatternData); case "MirrorSolid": return(ModelFeatureType.MirrorSolidData); case "MirrorStock": return(ModelFeatureType.MirrorPartData); case "MoveCopyBody": return(ModelFeatureType.MoveCopyBodyData); case "NetBlend": return(ModelFeatureType.BoundaryBossData); case "Punch": return(ModelFeatureType.IndentData); case "ReplaceFace": return(ModelFeatureType.ReplaceFaceData); case "RevCut": return(ModelFeatureType.RevolveData); case "Round fillet corner": return(ModelFeatureType.SimpleFilletData); case "Revolution": case "RevolutionThin": return(ModelFeatureType.RevolveData); case "Rib": return(ModelFeatureType.RibData); case "Rip": return(ModelFeatureType.RipData); case "Sculpt": return(ModelFeatureType.IntersectData); // NOTE: Obsolete and no interface case "Shape": return(ModelFeatureType.Shape); case "Shell": return(ModelFeatureType.ShellData); case "Split": return(ModelFeatureType.SplitBodyData); // NOTE: No interface; returned for a body created by splitting a part and saving the body to a part; you cannot access the data of a split body saved to a part case "SplitBody": return(ModelFeatureType.SplitBody); case "Stock": return(ModelFeatureType.DerivedPartData); case "Sweep": case "SweepCut": return(ModelFeatureType.SweepData); case "TablePattern": return(ModelFeatureType.TablePatternData); case "Thicken": case "ThickenCut": return(ModelFeatureType.ThickenData); case "VarFillet": return(ModelFeatureType.VariableFilletData); #endregion #region Drawing case "BendTableAchor": return(ModelFeatureType.TableAnchor); case "BomFeat": return(ModelFeatureType.Bom); case "BomTemplate": return(ModelFeatureType.TableAnchor); case "DetailCircle": return(ModelFeatureType.DetailCircle); case "DrBreakoutSectionLine": if (feature.SpecificFeature as IBrokenOutSectionFeatureData != null) { return(ModelFeatureType.BrokenOutSectionData); } else if (feature.SpecificFeature as IDrSection != null) { return(ModelFeatureType.DrSection); } return(ModelFeatureType.None); case "DrSectionLine": return(ModelFeatureType.DrSection); case "GeneralTableAnchor": case "HoleTableAnchor": return(ModelFeatureType.TableAnchor); case "LiveSection": return(ModelFeatureType.ReferencePlane); case "PunchTableAnchor": case "RevisionTableAnchor": case "WeldmentTableAnchor": case "WeldTableAnchor": return(ModelFeatureType.TableAnchor); #endregion #region Folder // NOTE: Obsolete and no interface case "BlockFolder": return(ModelFeatureType.BlockFolder); case "CommentsFolder": return(ModelFeatureType.CommentFolder); case "CosmeticWeldSubFolder": return(ModelFeatureType.CosmeticWeldBeadFolder); case "CutListFolder": case "FeatSolidBodyFolder": case "FeatSurfaceBodyFolder": return(ModelFeatureType.BodyFolder); case "FtrFolder": case "InsertedFeatureFolder": case "MateReferenceGroupFolder": return(ModelFeatureType.FeatureFolder); case "PosGroupFolder": return(ModelFeatureType.MateReference); case "ProfileFtrFolder": case "RefAxisFtrFolder": case "RefPlaneFtrFolder": return(ModelFeatureType.FeatureFolder); case "SolidBodyFolder": case "SubAtomFolder": case "SubWeldFolder": case "SurfaceBodyFolder": // As SubAtomFolder states "IBodyFolder if a body" we double check here if (feature.SpecificFeature as IBodyFolder != null) { return(ModelFeatureType.BodyFolder); } else { return(ModelFeatureType.None); } #endregion #region Miscellaneous case "Attribute": return(ModelFeatureType.Attribute); // NOTE: Obsolete and no interface case "BlockDef": return(ModelFeatureType.BlockDef); case "CurveInFile": return(ModelFeatureType.FreePointCurveData); // NOTE: No interface case "GridFeature": return(ModelFeatureType.Grid); case "LibraryFeature": return(ModelFeatureType.LibraryFeatureData); case "Scale": return(ModelFeatureType.ScaleData); case "Sensor": return(ModelFeatureType.Sensor); // NOTE: Obsolete case "ViewBodyFeature": return(ModelFeatureType.ViewBodyFeature); #endregion #region Mold case "Cavity": return(ModelFeatureType.CavityData); case "MoldCoreCavitySolids": return(ModelFeatureType.ToolingSplitData); case "MoldPartingGeom": return(ModelFeatureType.PartingSurfaceData); case "MoldPartLine": return(ModelFeatureType.PartingLineData); case "MoldShutOffSrf": return(ModelFeatureType.ShutOffSurfaceData); case "SideCore": return(ModelFeatureType.CoreData); case "XformStock": return(ModelFeatureType.DerivedPartData); #endregion #region Motion and Simulation case "AEM3DContact": return(ModelFeatureType.Simulation3DContactData); case "AEMGravity": return(ModelFeatureType.SimulationGravityData); case "AEMLinearDamper": return(ModelFeatureType.SimulationDamperData); case "AEMLinearMotor": return(ModelFeatureType.SimulationMotorData); case "AEMLinearSpring": return(ModelFeatureType.SimulationLinearSpringData); case "AEMRotationalMotor": return(ModelFeatureType.SimulationMotorData); case "AEMTorque": return(ModelFeatureType.SimulationForceData); case "AEMTorsionalDamper": return(ModelFeatureType.SimulationDamperData); // NOTE: No interface case "AEMTorsionalSpring": return(ModelFeatureType.TorsionalSpring); case "SimPlotFeature": return(ModelFeatureType.MotionPlotData); case "SimPlotXAxisFeature": case "SimPlotYAxisFeature": return(ModelFeatureType.MotionPlotAxisData); case "SimResultFolder": return(ModelFeatureType.MotionStudyResults); #endregion #region Reference Geometry case "CoordSys": return(ModelFeatureType.CoordinateSystemData); case "RefAxis": if (feature.SpecificFeature as IRefAxisFeatureData != null) { return(ModelFeatureType.ReferenceAxisData); } else if (feature.SpecificFeature as IRefAxis != null) { return(ModelFeatureType.ReferenceAxis); } else { return(ModelFeatureType.None); } case "RefPlane": return(ModelFeatureType.ReferencePlaneData); #endregion #region Scenes, Lights, Cameras case "AmbientLight": return(ModelFeatureType.Light); case "CameraFeature": return(ModelFeatureType.Camera); case "DirectionLight": case "PointLight": case "SpotLight": return(ModelFeatureType.Light); #endregion #region Sheet Metal case "SMBaseFlange": return(ModelFeatureType.BaseFlangeData); case "BreakCorner": case "CornerTrim": return(ModelFeatureType.BreakCornerData); case "CrossBreak": return(ModelFeatureType.CrossBreakData); case "EdgeFlange": return(ModelFeatureType.EdgeFlangeData); case "FlatPattern": return(ModelFeatureType.FlatPatternData); case "FlattenBends": return(ModelFeatureType.BendsData); case "Fold": return(ModelFeatureType.FoldsData); // NOTE: No interface case "FormToolInstance": return(ModelFeatureType.FormTool); case "Hem": return(ModelFeatureType.HemData); case "Jog": return(ModelFeatureType.JogData); case "LoftedBend": return(ModelFeatureType.LoftedBendsData); case "OneBend": return(ModelFeatureType.OneBendData); case "ProcessBends": return(ModelFeatureType.BendsData); case "SheetMetal": return(ModelFeatureType.SheetMetalData); case "SketchBend": return(ModelFeatureType.OneBendData); case "SM3dBend": return(ModelFeatureType.SketchedBendData); case "SMGusset": return(ModelFeatureType.SheetMetalGussetData); case "SMMiteredFlange": return(ModelFeatureType.MiterFlangeData); case "TemplateSheetMetal": return(ModelFeatureType.SheetMetalData); case "ToroidalBend": return(ModelFeatureType.OneBendData); case "UnFold": return(ModelFeatureType.FoldsData); #endregion #region Sketch case "3DProfileFeature": return(ModelFeatureType.Sketch); case "3DSplineCurve": if (feature.SpecificFeature as IReferencePointCurveFeatureData != null) { return(ModelFeatureType.ReferencePointCurveData); } if (feature.SpecificFeature as IReferenceCurve != null) { return(ModelFeatureType.ReferenceCurve); } else { return(ModelFeatureType.None); } case "CompositeCurve": if (feature.SpecificFeature as ICompositeCurveFeatureData != null) { return(ModelFeatureType.CompositeCurveData); } else if (feature.SpecificFeature as IReferenceCurve != null) { return(ModelFeatureType.ReferenceCurve); } else { return(ModelFeatureType.None); } case "ImportedCurve": if (feature.SpecificFeature as IImportedCurveFeatureData != null) { return(ModelFeatureType.ImportedCurveData); } else if (feature.SpecificFeature as IReferenceCurve != null) { return(ModelFeatureType.ReferenceCurve); } else { return(ModelFeatureType.None); } case "PLine": return(ModelFeatureType.SplitLineData); case "ProfileFeature": return(ModelFeatureType.Sketch); case "RefCurve": if (feature.SpecificFeature as IProjectionCurveFeatureData != null) { return(ModelFeatureType.ProjectionCurveData); } else if (feature.SpecificFeature as IReferenceCurve != null) { return(ModelFeatureType.ReferenceCurve); } else { return(ModelFeatureType.None); } case "SketchBlockDef": return(ModelFeatureType.SketchBlockDefinition); case "SketchBlockInst": return(ModelFeatureType.SketchBlockInstance); case "SketchHole": return(ModelFeatureType.SimpleHoleData); case "SketchPattern": return(ModelFeatureType.SketchPatternData); case "SketchBitmap": return(ModelFeatureType.SketchPicture); #endregion #region Surface // NOTE: No interface case "BlendRefSurface": return(ModelFeatureType.SurfaceLoft); case "ExtendRefSurface": return(ModelFeatureType.SurfaceExtendData); case "ExtruRefSurface": return(ModelFeatureType.SurfaceExtrudeData); case "FillRefSurface": return(ModelFeatureType.SurfaceFillData); case "FlattenSurface": return(ModelFeatureType.SurfaceFlattenData); case "MidRefSurface": return(ModelFeatureType.SurfaceMid); case "OffsetRefSuface": return(ModelFeatureType.SurfaceOffsetData); case "PlanarSurface": return(ModelFeatureType.SurfacePlanarData); case "RadiateRefSurface": return(ModelFeatureType.SurfaceRadiateData); // NOTE: No interface case "RefSurface": return(ModelFeatureType.SurfaceImported); case "RevolvRefSurf": return(ModelFeatureType.SurfaceRevolveData); case "RuledSrfFromEdge": return(ModelFeatureType.SurfaceRuledData); case "SewRefSurface": return(ModelFeatureType.SurfaceKnitData); case "SurfCut": return(ModelFeatureType.SurfaceCutData); // NOTE: No interface case "SweepRefSurface": return(ModelFeatureType.SurfaceSweep); case "TrimRefSurface": return(ModelFeatureType.SurfaceTrimData); // NOTE: No interface case "UnTrimRefSurf": return(ModelFeatureType.SurfaceUntrim); #endregion #region Weldment case "CosmeticWeldBead": return(ModelFeatureType.CosmeticWeldBeadData); case "EndCap": return(ModelFeatureType.EndCapData); case "Gusset": return(ModelFeatureType.GussetData); case "WeldBeadFeat": return(ModelFeatureType.WeldmentBeadData); case "WeldCornerFeat": return(ModelFeatureType.WeldmentTrimExtendData); case "WeldMemberFeat": return(ModelFeatureType.WeldmentMemberData); case "WeldmentTableFeat": return(ModelFeatureType.WeldmentCutListData); #endregion default: return(ModelFeatureType.None); } }