Why is it wrong to add the "sgATPIERCE" constraint?
I'm create a screw stem,The midpoint of the CenterRectangle is constrained to the endpoint of the helix,swept into a single screw.
The program works, but sometimes it reports errors
vba.net
'##################################
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
'
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
'
'Front Plane CreateCircle r=0.05m
skSegment = CType(swDoc.SketchManager.CreateCircleByRadius(0, 0, 0, 0.05), SketchSegment)
swDoc.ClearSelection2(True)
swDoc.SketchManager.InsertSketch(True)
'
'Front PlaneInsertHelix r=0.05m pitch 0.05m start angle 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)
'CreateCenterRectangle 0.056*0.007m
swDoc.ClearSelection2(True)
'Add "sgATPIERCE" Constraints
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) can not Select Helix/Spiral1
swDoc.SketchAddConstraints("sgATPIERCE") 'Add "sgATPIERCE" Constraints
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
’##################
boolstatus = swDoc.Extension.SelectByRay(0.05, 0, 0, 0, 0.05, 1, 0.001, 1, True, 0, 0) can not Select Helix/Spiral1 in "Plane1" but in "Front Plane" can Select Helix/Spiral1
--------------------------------
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: