Imports XCCLibrary
Imports SldWorks
Public Class Program

    Public Shared Sub Load_XCC(filesFolder As String, HLCtFolder As String)
        adminClass.loadSettings(filesFolder, HLCtFolder, "SolidWorks")
    End Sub

    Public Shared Sub Update_KB()
        adminClass.updateDatabase()
    End Sub

    Public Shared Sub Build_Grating(impTable As DataTable)
        XCCBaseClass.newDesign()
        ExternalClass.PopulateTamplateXMLGroup()

        Dim inst_ As Integer = 0
        Dim parentID As Integer = 0
        Dim gratingParameters As DataRow = impTable.Rows(0)
        inst_ = ExternalClass.instantiate_parts(parentID, gratingParameters("TYPE"), "") ' Ange pressure welded eller type A, Serrated?
        ExternalClass.modify_parameter_value("DP_GRATING_WIDTH", inst_, gratingParameters("WIDTH"))
        ExternalClass.modify_parameter_value("DP_GRATING_LENGTH", inst_, gratingParameters("LENGTH"))
        ExternalClass.modify_parameter_value("DP_LOADBAR_THICKNESS", inst_, gratingParameters("LOADBAR_THICKNESS"))
        ExternalClass.modify_parameter_value("DP_LOADBAR_HEIGHT", inst_, gratingParameters("LOADBAR_HEIGHT"))
        ExternalClass.modify_parameter_value("DP_LOADBAR_SPACING", inst_, gratingParameters("LOADBAR_SPACING"))
        ExternalClass.modify_parameter_value("DP_CROSSBAR_SPACING", inst_, gratingParameters("CROSSBAR_SPACING"))

        If gratingParameters("TYPE") = "pressure_welded" Then
            ExternalClass.modify_parameter_value("DP_CROSSBAR_DIAMETER", inst_, gratingParameters("CROSSBAR_DIAMETER"))
        Else
            ExternalClass.modify_parameter_value("DP_CROSSBAR_THICKNESS", inst_, gratingParameters("CROSSBAR_THICKNESS"))
            ExternalClass.modify_parameter_value("DP_CROSSBAR_HEIGHT", inst_, gratingParameters("CROSSBAR_HEIGHT"))
        End If

        importClass.copyGroupsByDataSet(ExternalClass.groupds_new)
        Dim grNr = groupClass.NewGroupID
        Dim designTable = designModel.selectDesign("*")
        Dim designName = designTable.rows(0)("designName")
        groupClass.InstantiateGroup(designName, grNr, "START")
    End Sub

    Public Shared Sub Export_SW(pointTable As DataTable)
        Dim swApp As SldWorks.SldWorks
        swApp = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application"), SldWorks.SldWorks)
        Dim Model As ModelDoc2
        Dim RootPoint(2) As Double
        Dim Normal(2) As Double
        swApp.UserControl = True
        'Create a new blank document
        Model = swApp.NewDocument("C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2020\templates\part.prtdot", 0, 0, 0)

        Dim swSkMgr As SketchManager
        Dim longstatus As Integer
        Dim boolstatus As Boolean

        swSkMgr = Model.SketchManager
        swSkMgr.InsertSketch(True)
        boolstatus = Model.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
        Model.ClearSelection2(True)

        For Each DR As DataRow In pointTable.Rows
            Dim skPoint As SketchPoint
            Dim pX = DR("X")
            Dim pY = DR("Y")
            skPoint = swSkMgr.CreatePoint(pX, pY, 0)
        Next

        For Each DR1 As DataRow In pointTable.Rows
            Dim skLine As SketchLine
            Dim rowIndex = pointTable.Rows.IndexOf(DR1)
            Dim DR2 As DataRow

            Try
                DR2 = pointTable.Rows(rowIndex + 1)
            Catch ex As Exception
                DR2 = pointTable.Rows(0)
            End Try

            Dim pX1 = DR1("X")
            Dim pY1 = DR1("Y")
            Dim pX2 = DR2("X")
            Dim pY2 = DR2("Y")

            skLine = swSkMgr.CreateLine(pX1, pY1, 0, pX2, pY2, 0)
        Next

        'Rename sketch?

        swSkMgr.InsertSketch(True)

        Dim status As Boolean
        Dim swModelDocExtension As ModelDocExtension

        swModelDocExtension = Model.Extension
        status = swModelDocExtension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

        Dim swFeatureMgr As FeatureManager
        swFeatureMgr = Model.FeatureManager
        Dim swFeature As Feature

        swFeature = swFeatureMgr.FeatureExtrusion3(True, False, False, 0, 0, 0.001, 0, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False)

        Dim iPart As PartDoc
        iPart = swApp.ActiveDoc
        Dim newName As String
        newName = "C:\Users\Anton\Documents\Exjobb\Temp" & "\TESTPART" & 1 & ".SLDPRT"
        longstatus = iPart.SaveAs3(newName, 0, 0)
    End Sub

    Public Shared Sub Create_Drawing()
        Dim swApp As SldWorks.SldWorks
        swApp = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application"), SldWorks.SldWorks)

        Dim iPart As PartDoc
        iPart = swApp.ActiveDoc

        Dim iDrawing As DrawingDoc
        Dim swSheetWidth As Double
        swSheetWidth = 0.42
        Dim swSheetHeight As Double
        swSheetHeight = 0.297
        iDrawing = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2020\templates\Drawing.drwdot", 12, swSheetWidth, swSheetHeight)

        Dim swSheet As Sheet
        swSheet = iDrawing.GetCurrentSheet()
        swSheet.SetProperties2(12, 12, 1, 1, False, swSheetWidth, swSheetHeight, True)
        swSheet.SetTemplateName("C:\ProgramData\SolidWorks\SOLIDWORKS 2020\lang\english\sheetformat\a3 - iso.slddrt")
        swSheet.ReloadTemplate(True)

        Dim myView As View
        myView = iDrawing.CreateDrawViewFromModelView3("C:\Users\Anton\Documents\Exjobb\Temp\TESTPART1.SLDPRT", "*Front", swSheetWidth / 2, swSheetHeight / 2, 0)

        Dim longstatus As Integer
        Dim newName As String
        newName = "C:\Users\Anton\Documents\Exjobb\Temp" & "\TESTDRAWING" & 1 & ".SLDDRW"
        longstatus = iDrawing.SaveAs3(newName, 0, 0)
    End Sub

End Class
