public void AddFeature(SldWorks SwApp, ModelDoc2 SwModel, Point2D P1, Point2D P2) { if (CheckUsing() == false) { return; } SwModel.ClearSelection2(true); double L = P2.x - P1.x; // stage length SketchManager sketchManager = SwModel.SketchManager; FeatureManager featureManager = SwModel.FeatureManager; Helper helper = new Helper(SwModel, SwApp); helper.select_feature("RefPlane", 2, false, 0); if (P1.x > 0) // if x cordinat of first point is 0 we use stantart plane, else - make new refplane by offset { SwModel.CreatePlaneAtOffset(P1.x, false); } sketchManager.InsertSketch(true); #region //Squre if (keyProfileType == (object)"Squre") // make squre stage for key (out) { sketchManager.CreateCenterRectangle(0, 0, 0, (keyProfileWidth / 2), (keyProfileWidth / 2), 0); featureManager.FeatureCut3(true, true, true, 0, 0, L, 0, false, false, false, false, 0, 0, false, false, false, false, false, true, true, true, true, false, 0, 0, false); // add cut feature } #endregion #region //Special if (keyProfileType == (object)"Speacial") // make special stage for key (out) { double H = Math.Sqrt((Math.Pow((2 * P1.y), 2) - Math.Pow(keyProfileWidth, 2))); sketchManager.Create3PointArc((keyProfileWidth / 2), (H / 2), 0, (-1 * keyProfileWidth / 2), (H / 2), 0, 0, P1.y, 0); // Top arc sketchManager.Create3PointArc((keyProfileWidth / 2), (-1 * H / 2), 0, (-1 * keyProfileWidth / 2), (-1 * H / 2), 0, 0, (-1 * P1.y), 0); // Bottom arc sketchManager.CreateLine((keyProfileWidth / 2), (H / 2), 0, (keyProfileWidth / 2), (-1 * H / 2), 0); // Left line sketchManager.CreateLine((-1 * keyProfileWidth / 2), (H / 2), 0, (-1 * keyProfileWidth / 2), (-1 * H / 2), 0); // Rigth line featureManager.FeatureCut3(true, true, true, 0, 0, L, 0, false, false, false, false, 0, 0, false, false, false, false, false, true, true, true, true, false, 0, 0, false); // add cut feature sketchManager.InsertSketch(false); } #endregion #region // Hexagon if (keyProfileType == (object)"Hexagon") // make squre stage for key (out) { sketchManager.CreatePolygon(0, 0, 0, 0, P1.y, 0, 6, false); featureManager.FeatureCut3(true, true, true, 0, 0, L, 0, false, false, false, false, 0, 0, false, false, false, false, false, true, true, true, true, false, 0, 0, false); // add cut feature } #endregion helper.HidePlanes(); }
//Microsoft.Office.Interop.MSProject //string[] args static void CreateSketch() { SldWorks swApp = new SldWorks(); //Feature myFeature; ModelDoc2 swModel = default(ModelDoc2); //RefPlane myRefPlane; SketchManager swSkMgr = default(SketchManager); FeatureManager swFeMgr = default(FeatureManager); //Feature swFeat; //Sketch swSketch; int longstatus = 0; bool boolstatus = false; swApp.ResetUntitledCount(0, 0, 0); swModel = (ModelDoc2)swApp.NewDocument("C:\\Users\\nemati\\Documents\\Visual Studio 2015\\Projects\\SWBlankApp\\SWBlankApp\\PartTemplate.SLDPRT", 0, 0, 0); swApp.ActivateDoc2("Part1", false, ref longstatus); swModel = (ModelDoc2)swApp.ActiveDoc; swSkMgr = swModel.SketchManager; swFeMgr = swModel.FeatureManager; swSkMgr.InsertSketch(true); //swFeMgr.FeatureExtruRefSurface() boolstatus = swModel.Extension.SelectByID2("Top Plane", "PLANE", -0.0553489443349025, 0.00330468607538553, 0.0269617286188933, false, 0, null, 0); swModel.ClearSelection2(true); if (swModel == null) { swApp.SendMsgToUser2("Ridi.", (int)swMessageBoxIcon_e.swMbWarning, (int)swMessageBoxBtn_e.swMbOk); return; } int modelType = 0; modelType = swModel.GetType(); if (modelType != (int)swDocumentTypes_e.swDocPART) { swApp.SendMsgToUser2("A part document must be active.", (int)swMessageBoxIcon_e.swMbWarning, (int)swMessageBoxBtn_e.swMbOk); return; } // Creating sample point SketchPoint skPoint = default(SketchPoint); skPoint = swSkMgr.CreatePoint(-100, 100, 0); // Creating sample arc SketchSegment skArc = default(SketchSegment); skArc = swSkMgr.Create3PointArc(0, 0, 0, 10, 10, 0, -10, 10, 0); // Creating sample circle SketchSegment skCircle = default(SketchSegment); skCircle = swSkMgr.CreateCircleByRadius(-20, 20, 0, 7.5); swSkMgr.InsertSketch(true); //swModel.SketchCircle(); Console.WriteLine(swApp.Visible.ToString()); Console.WriteLine(swApp.FrameWidth.ToString()); //swApp.CreateNewWindow(); swModel.SaveAs("C:\\Users\\nemati\\Documents\\Visual Studio 2015\\Projects\\SWBlankApp\\SWBlankApp\\SketchArc.SLDPRT"); //swApp. swApp.ExitApp(); //swApp = null; //swApp }
public void AddFeature(SldWorks SwApp, ModelDoc2 SwModel, Point2D P1, Point2D P2) { if (CheckUsing() == false) { return; } SwModel.ClearSelection2(true); object[] seating_types = new[] { "None", "Prismatic A", "Prismatic B", "Segmented" }; object[] operations_types = new[] { "None", "Chamfer", "Fillet" }; // array of operation names Helper helper = new Helper(SwModel, SwApp); FeatureManager featureManager = SwModel.FeatureManager; SketchManager sketchManager = SwModel.SketchManager; SelectionMgr selectionMgr = (SelectionMgr)SwModel.SelectionManager; Entity ent = default(Entity); Array facesArray = default(Array); Array edgesArray = default(Array); Feature swFeature = default(Feature); Face2 face2 = default(Face2); // neds to get edges of selected face double LStage = 0; double x_center = 0; double y_center = 0; double L = 0; double W = 0; bool append = false; LStage = P2.x - P1.x; // length of current stage x_center = P1.x + (LStage / 2) + seatingOffset; // center of current stage y_center = 0; L = x_center + seatingLength / 2; W = seatingWidth / 2; SwModel.ClearSelection2(true); #region // make prismatic A keyseat if (seatingType == seating_types[1]) //Prismatic A { helper.select_feature("RefPlane", 1, false, 0); SwModel.CreatePlaneAtOffset(P1.y, false); sketchManager.InsertSketch(true); sketchManager.CreateCenterRectangle(x_center, y_center, 0, L, W, 0); SwModel.FeatureManager.FeatureCut3(true, false, false, 0, 0, seatingDepth, 0, false, false, false, false, 0, 0, false, false, false, false, false, true, true, true, true, false, 0, 0, false); // add cut feature if (seatingOpearationType != operations_types[0]) // add chamfer or fillet, if none or value is 0 - pass { swFeature = (Feature)selectionMgr.GetSelectedObject6(1, -1); facesArray = (Array)swFeature.GetFaces(); ent = facesArray.GetValue(0) as Entity; ent.Select4(false, null); if (seatingOpearationType == operations_types[1]) { featureManager.InsertFeatureChamfer(4, 1, seatingChamferFilletValue, helper.ToRad(45), 0, 0, 0, 0); } if (seatingOpearationType == operations_types[2]) { SwModel.FeatureManager.FeatureFillet(195, seatingChamferFilletValue, 0, 0, null, null, null); } swFeature = selectionMgr.GetSelectedObject6(1, -1); swFeature.Select2(false, 4); //create selection for pattern append = true; } CreatePattern(helper, append, SwModel, swFeature, selectionMgr); } #endregion #region // make prismatic B keyseat if (seatingType == seating_types[2]) //Prismatic B { W = seatingWidth; //Width helper.select_feature("RefPlane", 1, false, 0); SwModel.CreatePlaneAtOffset(P1.y, false); sketchManager.InsertSketch(true); sketchManager.CreateSketchSlot((int)swSketchSlotCreationType_e.swSketchSlotCreationType_line, (int)swSketchSlotLengthType_e.swSketchSlotLengthType_CenterCenter, W, L - seatingLength, 0, 0, L, 0, 0, 0, 0, 0, 1, false); SwModel.FeatureManager.FeatureCut3(true, false, false, 0, 0, seatingDepth, 0, false, false, false, false, 0, 0, false, false, false, false, false, true, true, true, true, false, 0, 0, false); if (seatingOpearationType != operations_types[0]) // add chamfer or fillet, if none or value is 0 - pass { swFeature = (Feature)selectionMgr.GetSelectedObject6(1, -1); facesArray = (Array)swFeature.GetFaces(); ent = facesArray.GetValue(0) as Entity; ent.Select4(false, null); if (seatingOpearationType == operations_types[1]) { featureManager.InsertFeatureChamfer(4, 1, seatingChamferFilletValue, helper.ToRad(45), 0, 0, 0, 0); } if (seatingOpearationType == operations_types[2]) { SwModel.FeatureManager.FeatureFillet(195, seatingChamferFilletValue, 0, 0, null, null, null); } swFeature = selectionMgr.GetSelectedObject6(1, -1); swFeature.Select2(false, 4); //create selection for pattern append = true; } CreatePattern(helper, append, SwModel, swFeature, selectionMgr); } #endregion #region // make segmented seatkey if (seatingType == seating_types[3]) //Segmented { helper.select_feature("RefPlane", 0, false, 0); sketchManager.InsertSketch(true); sketchManager.Create3PointArc(L - seatingLength, P1.y, 0, L, P1.y, 0, x_center, (P1.y - seatingDepth), 0); sketchManager.CreateLine(L - seatingLength, P1.y, 0, L, P1.y, 0); //close sketsh SwModel.FeatureManager.FeatureCut3(true, false, false, 6, 0, (seatingWidth), 0, false, false, false, false, 0, 0, false, false, false, false, false, true, true, true, true, false, 0, 0, false); //cut from mide plane if (SeatingOpearationType != operations_types[0]) // add chamfer or fillet, if nono or value is 0 - pass { // select edges for chamfering or filleting swFeature = (Feature)selectionMgr.GetSelectedObject6(1, -1); facesArray = (Array)swFeature.GetFaces(); SelectData selectData = selectionMgr.CreateSelectData(); ent = facesArray.GetValue(2) as Entity; ent.Select4(false, selectData); face2 = selectionMgr.GetSelectedObject6(1, -1); edgesArray = (Array)face2.GetEdges(); ent = edgesArray.GetValue(0) as Entity; ent.Select4(false, null); ent = edgesArray.GetValue(2) as Entity; ent.Select4(true, null); // select edges for chamfering or filleting // MessageBox.Show("" + face2.GetEdgeCount()); if (seatingOpearationType == operations_types[1]) { featureManager.InsertFeatureChamfer(4, 1, seatingChamferFilletValue, helper.ToRad(45), 0, 0, 0, 0); } if (seatingOpearationType == operations_types[2]) { SwModel.FeatureManager.FeatureFillet(195, seatingChamferFilletValue, 0, 0, null, null, null); } swFeature = selectionMgr.GetSelectedObject6(1, -1); swFeature.Select2(false, 4); //create selection for pattern append = true; } CreatePattern(helper, append, SwModel, swFeature, selectionMgr); } #endregion helper.HidePlanes(); }