AnsweredAssumed Answered

Non Variable Values Change Depending on Variable Value

Question asked by Michael Benensky on May 8, 2019
Latest reply on May 8, 2019 by Michael Benensky

The macro I wrote creates different sketch than is coded.  It depends on the input value I enter as a variable.

For example I enter the length 250 (get similar correct results inputting length up to 400:

and it correctly sketches this:

However If I enter 500 I get this:

 

The point that shifts does not have a variable.  It should always be

   X9 = -0.016
    Y9 = -0.015
    Z9 = 0

for some reason  Y9 changes to -0.0105 when the RamLength variable reaches 500

 

I do not know why this is happening.  Any suggestions?

 

 

Here is my code from my module:

' ******************************************************************************
' C:\Users\mbenensky\AppData\Local\Temp\swx2876\Macro1.swb - macro recorded on 04/29/19 by mbenensky
' ******************************************************************************
'Macro to create 32mm Ram
Dim swApp As SldWorks.SldWorks 'Tells compiler to use solid works application & API
Dim Part As ModelDoc2 'declairs this model to be called "Part" as a ModelDoc2 that uses the ModelDoc2 Objects and methods

Sub Main()
'========================================================================================================================================
'This section opens new document using a template
Set swApp = Application.SldWorks 'will set swApp variable to SolidWorks application.
' For methods with many paramiters Iliek to create variables and set their values so that I make sure I got them all
    Dim TemplateName As String
    Dim PaperSize As Integer
    Dim Width As Double
    Dim Height As Double
   
    TemplateName = "P:\Solidworks\Shared_Templates\PART TEMPLATES\Fett-VM-2018.prtdot" ' This is a standsard FETT tempalte we like to use
    PaperSize = 0 ' this is not paper size zero, but a code defined in swDwgPaperSizes_e in this case it is a size
    Width = 0 ' this is not a width of zero, but a code defined in swDwgPaperSizes_e in this case it is a size
    Height = 0 ' this is not a height of zero, but a code defined in swDwgPaperSizes_e in this case it is a size
   
Set Part = swApp.NewDocument(TemplateName, PaperSize, Width, Height) 'don't know pourpouse of this line but was in program I copied from and I am afraid to remove it
Set Part = swApp.ActiveDoc 'don't know pourpouse of this line but was in program I copied from and I am afraid to remove it
 
'========================================================================================================================================
' This section creates sketch plane

boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0) ' Selects Front Plane
Part.SketchManager.InsertSketch True 'This method Inserts sketch plane
'========================================================================================================================================
'Closing all open solid works documents
'Dim instance As ISldWorks
'Dim IncludeUnsaved As Boolean
'IncludeUnsaved = False

'Value = swApp.CloseAllDocuments(IncludeUnsaved)
'Shos a userform for a ram
UserForm1.Show

'========================================================================================================================================
End Sub 'Ends main Sub
'========================================================================================================================================
'Creating Public Sub to create the Solid model of the Ram
Public Sub CreateRamModel(InputLength As Double)
'========================================================================================================================================
'========================================================================================================================================
'Turning off Sketch Inferences
'It was causing problems when I was drawing the Chamfers
'swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swSketchInference, False
' Setting Cylinderlength variable
Dim RamLength As Double
RamLength = InputLength
UserForm1.Hide

'========================================================================================================================================
' This section creates centerline for ram
Dim CenterLine As SketchSegment
    Dim X1 As Double
    Dim Y1 As Double
    Dim Z1 As Double
    Dim X2 As Double
    Dim Y2 As Double
    Dim Z2 As Double
    X1 = 0
    Y1 = 0.025
    Z1 = 0
    X2 = 0
    Y2 = 0 - (RamLength / 1000) - 0.025
    Z2 = 0
Dim EndChamferSegment As SketchSegment
Set CenterLine = Part.SketchManager.CreateCenterLine(X1, Y1, Z1, X2, Y2, Z2)
'========================================================================================================================================
' Zoom To Fit
Part.ViewZoomtofit2
   
'========================================================================================================================================
' This section draws the profile of the ram
   
   
    Dim X3 As Double
    Dim Y3 As Double
    Dim Z3 As Double
    Dim X4 As Double
    Dim Y4 As Double
    Dim Z4 As Double
    Dim X5 As Double
    Dim Y5 As Double
    Dim Z5 As Double
    Dim X6 As Double
    Dim Y6 As Double
    Dim Z6 As Double
    Dim X7 As Double
    Dim Y7 As Double
    Dim Z7 As Double
    Dim X8 As Double
    Dim Y8 As Double
    Dim Z8 As Double
    Dim X9 As Double
    Dim Y9 As Double
    Dim Z9 As Double
    Dim X10 As Double
    Dim Y10 As Double
    Dim Z10 As Double
    Dim X11 As Double
    Dim Y11 As Double
    Dim Z11 As Double
   
    X3 = 0
    Y3 = 0
    Z3 = 0
   
    X4 = -0.008
    Y4 = 0
    Z4 = 0
     
    X5 = -0.009
    Y5 = -0.001
    Z5 = 0
   
    X6 = -0.009
    Y6 = -0.0105
    Z6 = 0
   
    X7 = -0.00785
    Y7 = -0.01165
    Z7 = 0
   
    X8 = -0.00785
    Y8 = -0.015
    Z8 = 0
   
    X9 = -0.016
    Y9 = -0.015
    Z9 = 0
   
    X9 = -0.016
    Y9 = -0.015
    Z9 = 0
   
    X10 = -0.016
    Y10 = 0 - (RamLength / 1000)
    Z10 = 0
   
    X11 = 0
    Y11 = 0 - (RamLength / 1000)
    Z11 = 0
   
Dim EndSegment As SketchSegment
Set EndSegment = Part.SketchManager.CreateLine(X3, Y3, Z3, X4, Y4, Z4)
Dim ChamferForEdgeSegment As SketchSegment
Set ChamferForEdgeSegment = Part.SketchManager.CreateLine(X4, Y4, Z4, X5, Y5, Z5)
'Dim EndChamferSegment As SketchSegment
'Set EndChamferSegment = Part.SketchManager.CreateLine(X4, Y4, Z4, X5, Y5, Z5)
Dim ThreadEdgeSegment As SketchSegment
Set ThreadEdgeSegment = Part.SketchManager.CreateLine(X5, Y5, Z5, X6, Y6, Z6)
Dim ThreadChamferSegment As SketchSegment
Set ThreadChamferSegment = Part.SketchManager.CreateLine(X6, Y6, Z6, X7, Y7, Z7)
Dim UndercutRootSegment As SketchSegment
Set UndercutRootSegment = Part.SketchManager.CreateLine(X7, Y7, Z7, X8, Y8, Z8)
Dim MainShoulderSegment As SketchSegment
Set MainShoulderSegment = Part.SketchManager.CreateLine(X8, Y8, Z8, X9, Y9, Z9)
Dim LargeODSegment As SketchSegment
Set LargeODSegment = Part.SketchManager.CreateLine(X9, Y9, Z9, X10, Y10, Z10)
Dim ActuatorEndSegment As SketchSegment
Set ActuatorEndSegment = Part.SketchManager.CreateLine(X10, Y10, Z10, X11, Y11, Z11)
'Dim CloseSketchSegment As SketchSegment
'Set CloseSketchSegment = Part.SketchManager.CreateLine(X3, Y3, Z3, X11, Y11, Z11)

'========================================================================================================================================
'Turning on Sketch Inferences
'finished setting setting s back to normal
swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swSketchInference, True
'========================================================================================================================================
'testing see if doing thie will alow it to revolve
boolstatus = Part.DeSelectByID("Line9", "SKETCHSEGMENT", -6.61063345186036E-03, -0.25, 0) 'deselects last segment
boolstatus = Part.Extension.SelectByID2("Line9", "SKETCHSEGMENT", -6.61063345186036E-03, -0.248590746660397, 0, True, 0, Nothing, 0) 'selects last segment
boolstatus = Part.Extension.SelectByID2("Line8", "SKETCHSEGMENT", -0.016533978074228, -0.230548301892456, 0, True, 0, Nothing, 0) 'selects next to last segment
Part.SketchAddConstraints "sgPERPENDICULAR" 'makes the two selected segments perpendicular
Part.ClearSelection2 True ' cleears part selected
boolstatus = Part.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0) 'selecting the sketch
boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0.025, 0, True, 0, Nothing, 0) ' not deselecting anything
'========================================================================================================================================
'revolving
'Named View
Part.ShowNamedView2 "*Trimetric", 8
Part.ViewZoomtofit2
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0.025, 0, True, 16, Nothing, 0)
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureRevolve2(True, True, False, False, False, False, 0, 0, 6.2831853071796, 0, False, False, 0.01, 0.01, 0, 0, 0, True, True, True)
Part.SelectionManager.EnableContourSelection = True
'========================================================================================================================================
End Sub ' End Public Sub CreateRamModel

Outcomes