//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 PointD draw(SketchManager sm) { sketch = sm.CreatePoint(X, Y, Z); sketch.Select(false); return(this); }
/// <summary> /// 这里提供获取当前装配体的 bounding box /// </summary> /// <returns>返回 LxWxH</returns> private string GetActionAssemlbyBOX() { BoxSize newboxSize = new BoxSize(); ModelDoc2 swModel = default(ModelDoc2); double L = 0; double W = 0; double H = 0; double[] BoxFeatureDblArray = new double[7]; double[] BoxFaceDblArray = new double[7]; SketchPoint[] swSketchPt = new SketchPoint[9]; SketchSegment[] swSketchSeg = new SketchSegment[13]; swModel = (ModelDoc2)iswApp.IActiveDoc2; AssemblyDoc assemblyDoc = (AssemblyDoc)swModel; BoxFeatureDblArray = (double[])assemblyDoc.GetBox((int)swBoundingBoxOptions_e.swBoundingBoxIncludeRefPlanes); L = (BoxFeatureDblArray[3] - BoxFeatureDblArray[0]) * 1000; W = (BoxFeatureDblArray[4] - BoxFeatureDblArray[1]) * 1000; H = (BoxFeatureDblArray[5] - BoxFeatureDblArray[2]) * 1000; List <double> myList = new List <double> { L, W, H }; myList.Sort(); newboxSize.Length = myList[2]; newboxSize.Weigth = myList[1]; newboxSize.Height = myList[0]; bool b = swModel.Extension.SelectByID2("Size", "SKETCH", 0, 0, 0, false, 0, null, 0); if (b == false) { swModel.Insert3DSketch2(true); swModel.SetAddToDB(true); swModel.SetDisplayWhenAdded(false); SketchManager swSketchMgr = default(SketchManager); swSketchMgr = (SketchManager)swModel.SketchManager; // Draw points at each corner of bounding box swSketchPt[0] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[1], BoxFeatureDblArray[5]); swSketchPt[1] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[1], BoxFeatureDblArray[5]); swSketchPt[2] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[1], BoxFeatureDblArray[2]); swSketchPt[3] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[1], BoxFeatureDblArray[2]); swSketchPt[4] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[4], BoxFeatureDblArray[5]); swSketchPt[5] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[4], BoxFeatureDblArray[5]); swSketchPt[6] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[4], BoxFeatureDblArray[2]); swSketchPt[7] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[4], BoxFeatureDblArray[2]); // Now draw bounding box swSketchSeg[0] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[0].X, swSketchPt[0].Y, swSketchPt[0].Z, swSketchPt[1].X, swSketchPt[1].Y, swSketchPt[1].Z); swSketchSeg[1] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[1].X, swSketchPt[1].Y, swSketchPt[1].Z, swSketchPt[2].X, swSketchPt[2].Y, swSketchPt[2].Z); swSketchSeg[2] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[2].X, swSketchPt[2].Y, swSketchPt[2].Z, swSketchPt[3].X, swSketchPt[3].Y, swSketchPt[3].Z); swSketchSeg[3] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[3].X, swSketchPt[3].Y, swSketchPt[3].Z, swSketchPt[0].X, swSketchPt[0].Y, swSketchPt[0].Z); swSketchSeg[4] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[0].X, swSketchPt[0].Y, swSketchPt[0].Z, swSketchPt[4].X, swSketchPt[4].Y, swSketchPt[4].Z); swSketchSeg[5] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[1].X, swSketchPt[1].Y, swSketchPt[1].Z, swSketchPt[5].X, swSketchPt[5].Y, swSketchPt[5].Z); swSketchSeg[6] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[2].X, swSketchPt[2].Y, swSketchPt[2].Z, swSketchPt[6].X, swSketchPt[6].Y, swSketchPt[6].Z); swSketchSeg[7] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[3].X, swSketchPt[3].Y, swSketchPt[3].Z, swSketchPt[7].X, swSketchPt[7].Y, swSketchPt[7].Z); swSketchSeg[8] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[4].X, swSketchPt[4].Y, swSketchPt[4].Z, swSketchPt[5].X, swSketchPt[5].Y, swSketchPt[5].Z); swSketchSeg[9] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[5].X, swSketchPt[5].Y, swSketchPt[5].Z, swSketchPt[6].X, swSketchPt[6].Y, swSketchPt[6].Z); swSketchSeg[10] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[6].X, swSketchPt[6].Y, swSketchPt[6].Z, swSketchPt[7].X, swSketchPt[7].Y, swSketchPt[7].Z); swSketchSeg[11] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[7].X, swSketchPt[7].Y, swSketchPt[7].Z, swSketchPt[4].X, swSketchPt[4].Y, swSketchPt[4].Z); swModel.SetDisplayWhenAdded(true); swModel.SetAddToDB(false); //string actionSketchname = swModel.SketchManager.ActiveSketch.Name; swModel.Insert3DSketch2(true); swModel.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, true, false, "Size"); swModel.ClearSelection2(true); swModel.Extension.SelectByID2("Size", "SKETCH", 0, 0, 0, false, 0, null, 0); swModel.BlankSketch(); swModel.ClearSelection2(true); } BoxSize boxSize = new BoxSize(); boxSize.Length = Math.Round(newboxSize.Length, 0); boxSize.Weigth = Math.Round(newboxSize.Weigth, 0); boxSize.Height = Math.Round(newboxSize.Height, 0); string proText = string.Join("x", boxSize.Length, boxSize.Weigth, boxSize.Height); return(proText); }
public void GetBoundingBox(out BoxSize boxSize) { BoxSize newboxSize = new BoxSize(); ModelDoc2 swModel = default(ModelDoc2); double L = 0; double W = 0; double H = 0; double[] BoxFeatureDblArray = new double[7]; SketchManager swSketchMgr = default(SketchManager); SketchPoint[] swSketchPt = new SketchPoint[9]; SketchSegment[] swSketchSeg = new SketchSegment[13]; swModel = (ModelDoc2)iswApp.IActiveDoc2; PartDoc swPart = (PartDoc)swModel; double[] BoxFaceDblArray = new double[7]; BoxFeatureDblArray = (double[])swPart.GetPartBox(true); Debug.Print(" Pt1 = " + "(" + BoxFeatureDblArray[0] * 1000.0 + ", " + BoxFeatureDblArray[1] * 1000.0 + ", " + BoxFeatureDblArray[2] * 1000.0 + ") mm"); Debug.Print(" Pt2 = " + "(" + BoxFeatureDblArray[3] * 1000.0 + ", " + BoxFeatureDblArray[4] * 1000.0 + ", " + BoxFeatureDblArray[5] * 1000.0 + ") mm"); L = (BoxFeatureDblArray[3] - BoxFeatureDblArray[0]) * 1000; W = (BoxFeatureDblArray[4] - BoxFeatureDblArray[1]) * 1000; H = (BoxFeatureDblArray[5] - BoxFeatureDblArray[2]) * 1000; List <double> myList = new List <double> { L, W, H }; myList.Sort(); newboxSize.Length = myList[2]; newboxSize.Weigth = myList[1]; newboxSize.Height = myList[0]; swModel.Insert3DSketch2(true); swModel.SetAddToDB(true); swModel.SetDisplayWhenAdded(false); swSketchMgr = (SketchManager)swModel.SketchManager; // Draw points at each corner of bounding box swSketchPt[0] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[1], BoxFeatureDblArray[5]); swSketchPt[1] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[1], BoxFeatureDblArray[5]); swSketchPt[2] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[1], BoxFeatureDblArray[2]); swSketchPt[3] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[1], BoxFeatureDblArray[2]); swSketchPt[4] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[4], BoxFeatureDblArray[5]); swSketchPt[5] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[4], BoxFeatureDblArray[5]); swSketchPt[6] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[4], BoxFeatureDblArray[2]); swSketchPt[7] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[4], BoxFeatureDblArray[2]); // Now draw bounding box swSketchSeg[0] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[0].X, swSketchPt[0].Y, swSketchPt[0].Z, swSketchPt[1].X, swSketchPt[1].Y, swSketchPt[1].Z); swSketchSeg[1] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[1].X, swSketchPt[1].Y, swSketchPt[1].Z, swSketchPt[2].X, swSketchPt[2].Y, swSketchPt[2].Z); swSketchSeg[2] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[2].X, swSketchPt[2].Y, swSketchPt[2].Z, swSketchPt[3].X, swSketchPt[3].Y, swSketchPt[3].Z); swSketchSeg[3] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[3].X, swSketchPt[3].Y, swSketchPt[3].Z, swSketchPt[0].X, swSketchPt[0].Y, swSketchPt[0].Z); swSketchSeg[4] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[0].X, swSketchPt[0].Y, swSketchPt[0].Z, swSketchPt[4].X, swSketchPt[4].Y, swSketchPt[4].Z); swSketchSeg[5] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[1].X, swSketchPt[1].Y, swSketchPt[1].Z, swSketchPt[5].X, swSketchPt[5].Y, swSketchPt[5].Z); swSketchSeg[6] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[2].X, swSketchPt[2].Y, swSketchPt[2].Z, swSketchPt[6].X, swSketchPt[6].Y, swSketchPt[6].Z); swSketchSeg[7] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[3].X, swSketchPt[3].Y, swSketchPt[3].Z, swSketchPt[7].X, swSketchPt[7].Y, swSketchPt[7].Z); swSketchSeg[8] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[4].X, swSketchPt[4].Y, swSketchPt[4].Z, swSketchPt[5].X, swSketchPt[5].Y, swSketchPt[5].Z); swSketchSeg[9] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[5].X, swSketchPt[5].Y, swSketchPt[5].Z, swSketchPt[6].X, swSketchPt[6].Y, swSketchPt[6].Z); swSketchSeg[10] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[6].X, swSketchPt[6].Y, swSketchPt[6].Z, swSketchPt[7].X, swSketchPt[7].Y, swSketchPt[7].Z); swSketchSeg[11] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[7].X, swSketchPt[7].Y, swSketchPt[7].Z, swSketchPt[4].X, swSketchPt[4].Y, swSketchPt[4].Z); swModel.SetDisplayWhenAdded(true); swModel.SetAddToDB(false); //string actionSketchname = swModel.SketchManager.ActiveSketch.Name; swModel.Insert3DSketch2(true); swModel.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, true, false, "Size"); swModel.ClearSelection2(true); boxSize.Length = newboxSize.Length; boxSize.Weigth = newboxSize.Weigth; boxSize.Height = newboxSize.Height; // swModel.Extension.CustomPropertyManager[""].Delete("尺寸"); swModel.Extension.CustomPropertyManager[""].Add3("尺寸", 30, "aaaa", 1); // swModel.AddCustomInfo("尺寸","Text", boxSize.Length.ToString()); swModel.Save(); }