5 Replies Latest reply on Jul 13, 2016 3:41 PM by Edwin Thompson

    Trouble with example code

    Edwin Thompson

      I'm trying to follow an example but am not sure how to address the error:

       

      The code that was copied is shown below.  The error is shown at the bottom.

       

      Any ideas how to fix this are very appreciated.  Any explanation of the "bigger picture" would also be very helpful.... thank you.

       

      'following dim's are for Holewizard example

              Dim swModel As ModelDoc2

              Dim swModelDocExt As ModelDocExtension

              Dim swFeatMgr As FeatureManager

              Dim swFeat As Feature

              Dim swSketchMgr As SketchManager

              Dim sketchLines As Object

              Dim status As Integer

              Dim boolstatus As Boolean

              Dim P1(2) As Double

              Dim P2(2) As Double

              Dim P3(2) As Double

       

              ' Create the model for the wizard hole

              swApp.ResetUntitledCount(0, 0, 0)

              swModel = swApp.NewDocument("C:\Documents and Settings\All Users\Application Data\SOLIDWORKS\SOLIDWORKS 2014\templates\Slot Test Part.prtdot", 0, 0, 0)

              swApp.ActivateDoc2("Part1", False, status)

              swModel = swApp.ActiveDoc

              swSketchMgr = swModel.SketchManager

              swModelDocExt = swModel.Extension

              swFeatMgr = swModel.FeatureManager

              sketchLines = swSketchMgr.CreateCornerRectangle(-0.05096498314664, 0.05060941349678, 0, 0.1021670127265, -0.05037236706354, 0)

              boolstatus = swModelDocExt.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)

              boolstatus = swModelDocExt.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)

              boolstatus = swModelDocExt.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)

              boolstatus = swModelDocExt.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)

       

              swFeat = swFeatMgr.FeatureExtrusion2(True, False, False, 0, 0, 0.381, 0.381, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, True, True, True, 0, 0, False)

       

              'Create three points for the reference plane

       

              P1(0) = -0.0141556764402858

              P1(1) = 0.00194061273859598

              P1(2) = 0

              P2(0) = -0.0141556764402858

              P2(1) = 0.00194061273859598

              P2(2) = 1

              P3(0) = -0.149976101832345

              P3(1) = -0.988792859011662

              P3(2) = 0

       

              'Create the reference plane

              swModel.CreatePlaneFixed2(P1, P2, P3, False)

       

              'Select the reference plane

              boolstatus = swModelDocExt.SelectByID2("Plane1", "PLANE", -0.0156784487003801, -0.00916715285390111, 0.0558270998665543, False, 0, Nothing, 0)

       

              ' Create the hole wizard hole

             swFeat = swFeatMgr.HoleWizard5(swFeatMgr.swWzdGeneralHoleTypes.swWzdCounterSink, swModel.swWzdHoleStandards.swStandardAnsiMetric, swModel.swWzdHoleStandardFastenerTypes.swStandardAnsiMetricFlatHead82, "M2", swModel.swEndConditions.swEndCondThroughAll, 0.0102, 0.010312189893273, 0, 0.0044, 1.57079632679489, 0.000152189893272978, 0, 2.05948851735331, 0, 0, 0, 1, 0, 0, 0, "", False, True, True, True, True, False)

       

        • Re: Trouble with example code
          Viktor Bovzdarenko

          Hi Edwin

          There were a few issues with the macro

          try corrected one with the following result:

           

           

          Option Explicit

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

           

           

          Sub Main()

              Set swApp = Application.SldWorks

              swApp.ResetUntitledCount 0, 0, 0

              Set swModel = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2015\templates\Part.prtdot", 0, 0, 0)

              Set swModel = swApp.ActiveDoc

              Dim sketchLines As Variant: sketchLines = swModel.SketchManager.CreateCornerRectangle(-0.05096498314664, 0.05060941349678, 0, 0.1021670127265, -0.05037236706354, 0)

              swModel.SketchManager.InsertSketch True

              Dim boolstatus As Boolean: boolstatus = swModel.extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

              Dim swFeat As Feature: Set swFeat = swModel.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.381, 0.381, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, True, True, True, 0, 0, False)

              Dim P1(2) As Double

              Dim P2(2) As Double

              Dim P3(2) As Double

              P1(0) = -1.41556764402858E-02

              P1(1) = 1.94061273859598E-03

              P1(2) = 0

              P2(0) = -1.41556764402858E-02

              P2(1) = 1.94061273859598E-03

              P2(2) = 1

              P3(0) = -0.149976101832345

              P3(1) = -0.988792859011662

              P3(2) = 0

              swModel.CreatePlaneFixed2 P1, P2, P3, False

              boolstatus = swModel.extension.SelectByID2("Plane1", "PLANE", -1.56784487003801E-02, -9.16715285390111E-03, 5.58270998665543E-02, False, 0, Nothing, 0)

              Set swFeat = swModel.FeatureManager.HoleWizard5(1, 1, 36, "M2", 1, 0.0102, 0.010312189893273, 0, 0.0044, 1.57079632679489, 1.52189893272978E-04, 0, 2.05948851735331, 0, 0, 0, 1, 0, 0, 0, "", False, True, True, True, True, False)

          End Sub

            • Re: Trouble with example code
              Edwin Thompson

              Everything looks good except for the last line:

               

              swFeat = swFeatMgr.HoleWizard5(swWzdGeneralHoleTypes_e.swWzdCounterSink, swWzdHoleStandards_e.swStandardAnsiMetric, swWzdHoleStandardFastenerTypes_e.swStandardAnsiMetricFlatHead82, "M2", swEndConditions_e.swEndCondThroughAll, 0.0102, 0.010312189893273, 0, 0.0044, 1.57079632679489, 0.000152189893272978, 0, 2.05948851735331, 0, 0, 0, 1, 0, 0, 0, "", False, True, True, True, True, False)

              I don't understand the "_e" term of the code but VB.net is claiming that they are not declared.

              Can you please explain what these modifiers are intended to do.

              I did remove the "_e" and added the "swFeatMgr." to each of the terms.  However the line still results in an error.

               

              swFeat = swFeatMgr.HoleWizard5(swFeatMgr.swWzdGeneralHoleTypes.swWzdCounterSink, swFeatMgr.swWzdHoleStandards.swStandardAnsiMetric, swFeatMgr.swWzdHoleStandardFastenerTypes.swStandardAnsiMetricFlatHead82, "M2", swFeatMgr.swEndConditions.swEndCondThroughAll, 0.0102, 0.010312189893273, 0, 0.0044, 1.57079632679489, 0.000152189893272978, 0, 2.05948851735331, 0, 0, 0, 1, 0, 0, 0, "", False, True, True, True, True, False)

               

              I really appreciate your help.

            • Re: Trouble with example code
              Edwin Thompson

              It has been 4 years since I worked on this project, It is amazing what one can forget.  Thank you for the help.