|
经典图书 程序创建一个单螺杆
螺杆叶面是矩形,用中心矩形的中心对其螺旋线的一个端点,做穿透约束。
打开solid works 然后在vs中运行代码可以通过,但是再次运行时就报错。
注销电脑,从新进入又正常。
不打开solid works单独运行代码报错,打开sldworks,运行代码有时报错,关闭sldworks,重启sldworks本身报错,
关闭sldworks,重启sldworks本身报错
错误详情
未处理System.Runtime.InteropServices.COMException
ErrorCode=-2147417851
HResult=-2147417851
Message=服务器出现意外情况。 (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT))
Source=swCreate single-screw
StackTrace:
在 SldWorks.IModelDoc2.SketchAddConstraints(String IdStr)
在 swCreate_single_screw.Form1.Button1_Click(Object sender, EventArgs e) 位置 D:\NXopenCode\swCreate single-screw\swCreate single-screw\Form1.vb:行号 62
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
在 swCreate_single_screw.My.MyApplication.Main(String[] Args) 位置 17d14f5c-a337-4978-8281-53493378c1071.vb:行号 81
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
代码如下:
- Imports SldWorks
- Imports SwConst
- Imports System.Runtime.InteropServices
- Imports System
- Public Class Form1
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim swApp As SldWorks.SldWorks
- Dim swDoc As ModelDoc2 = Nothing
- Dim swPart As PartDoc = Nothing
- Dim swDrawing As DrawingDoc = Nothing
- Dim swAssembly As AssemblyDoc = Nothing
- Dim boolstatus As Boolean = False
- Dim longstatus As Integer = 0
- Dim longwarnings As Integer = 0
- '
- '建立于solid works的连接
- swApp = CreateObject("SldWorks.application")
- Dim swSheetWidth As Double
- swSheetWidth = 0
- Dim swSheetHeight As Double
- swSheetHeight = 0
- swDoc = CType(swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2017\templates\gb_part.prtdot", 0, swSheetWidth, swSheetHeight), ModelDoc2)
- swPart = swDoc
- swApp.ActivateDoc2("Part3", False, longstatus)
- swDoc = CType(swApp.ActiveDoc, ModelDoc2)
- Dim myModelView As ModelView = Nothing
- myModelView = CType(swDoc.ActiveView, ModelView)
- myModelView.FrameState = CType(swWindowState_e.swWindowMaximized, Integer)
- boolstatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
- swDoc.SketchManager.InsertSketch(True)
- swDoc.ClearSelection2(True)
- Dim skSegment As SketchSegment = Nothing
- '前视基准面 画圆 r=0.05m
- skSegment = CType(swDoc.SketchManager.CreateCircleByRadius(0, 0, 0, 0.05), SketchSegment)
- swDoc.ClearSelection2(True)
- swDoc.SketchManager.InsertSketch(True)
- '前视基准面 插入螺旋线 半径0.05m 导程0.05m 起始角度0
- swDoc.InsertHelix(False, True, False, True, 0, 0.05, 0.05, 1, 0, 0)
- boolstatus = swDoc.Extension.SelectByRay(0.05, 0, 0, 0, 0.05, 1, 0.001, 1, True, 0, 0)
- boolstatus = swDoc.Extension.SelectByID2("Unknown", "POINTREF", 0.049999999999999961, 0, 0.049999999999999975, True, 1, Nothing, 0)
- Dim myRefPlane As RefPlane = Nothing
- myRefPlane = CType(swDoc.FeatureManager.InsertRefPlane(2, 0, 4, 0, 0, 0), RefPlane)
- swDoc.ClearSelection2(True)
- '在螺旋线的端点上插入参考平面,参考平面垂直螺旋线
- boolstatus = swDoc.Extension.SelectByID2("Plane1", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
- swDoc.SketchManager.InsertSketch(True)
- swDoc.ClearSelection2(True)
- boolstatus = swDoc.Extension.SetUserPreferenceToggle(CType(swUserPreferenceToggle_e.swSketchAddConstToRectEntity, Integer), CType(swUserPreferenceOption_e.swDetailingNoOptionSpecified, Integer), True)
- boolstatus = swDoc.Extension.SetUserPreferenceToggle(CType(swUserPreferenceToggle_e.swSketchAddConstLineDiagonalType, Integer), CType(swUserPreferenceOption_e.swDetailingNoOptionSpecified, Integer), True)
- Dim vSkLines As Array = Nothing
- vSkLines = CType(swDoc.SketchManager.CreateCenterRectangle(0, 0.008, 0, 0.028, 0.0115, 0), Array)
- '画中心点正方形0.056*0.007m
- swDoc.ClearSelection2(True)
- '清除选择然后选择正方形中心点和螺旋线,添加穿透约束
- boolstatus = swDoc.Extension.SelectByID2("Point1", "SKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
- boolstatus = swDoc.Extension.SelectByID2("Helix/Spiral1", "REFERENCECURVES", 0, 0, 1, True, 0, Nothing, 0)
- 'boolstatus = swDoc.Extension.SelectByRay(0.05, 0, 0, 0, 0.05, 1, 0.001, 1, True, 0, 0) 选不中螺旋线
- 'MsgBox("选择螺旋线准备穿透约束")
- swDoc.SketchAddConstraints("sgATPIERCE") '添加穿透约束
- swDoc.ClearSelection2(True)
- swDoc.SketchManager.InsertSketch(True)
- boolstatus = swDoc.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, False, 1, Nothing, 0)
- boolstatus = swDoc.Extension.SelectByID2("Helix/Spiral1", "REFERENCECURVES", 0, 0, 0, True, 4, Nothing, 0)
- Dim myFeature As Feature = Nothing
- myFeature = CType(swDoc.FeatureManager.InsertProtrusionSwept4(False, False, 0, False, False, 1, 1, False, 0, 0, 0, 10, True, True, True, 0, True, False, 0.01, False), Feature)
- swDoc.ClearSelection2(True)
- '
- 'Save As
- longstatus = swDoc.SaveAs3("D:\UG\Part3.SLDPRT", 0, 2)
- '
- 'Close Document
- swPart = Nothing
- swDoc = Nothing
- swApp.CloseDoc("Part3.SLDPRT")
- End Sub
- End Class
复制代码
|
|