Imports XCCLibrary
Imports SldWorks
Imports System.Diagnostics

Public Class Model_3D
    Private Shared designName As String
    Public Shared Sub BuildGrating()
        Dim exportTable As DataTable
        exportTable = User_Input.Create_ExportTable()

        XCCBaseClass.newDesign()
        ExternalClass.PopulateTamplateXMLGroup()

        Dim inst_ As Integer
        Dim parentID As Integer = 0
        Dim gratingParameters As DataRow = exportTable.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

        'Instantiate recess rectangle
        parentID = inst_
        For Each recessDR As DataRow In Data.recessData.Rows
            Inst_Recesses(parentID, gratingParameters, recessDR)
        Next

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

        Recess_Indent()
    End Sub

    Private Shared Sub Inst_Recesses(parentID As Integer, gratingParameters As DataRow, recessDR As DataRow)
        Dim inst_ As Integer

        If recessDR("RECESS TYPE") = "SQUARE" Then
            Dim squareL As Integer = recessDR("LENGTH") + gratingParameters("LOADBAR_THICKNESS")
            Dim squareW As Integer = recessDR("WIDTH") + gratingParameters("LOADBAR_THICKNESS")

            inst_ = ExternalClass.instantiate_parts(parentID, "recess_rectangle", "")

            ExternalClass.modify_parameter_value("DP_LENGTH", inst_, squareL + gratingParameters("LOADBAR_THICKNESS"))
            ExternalClass.modify_parameter_value("DP_WIDTH", inst_, squareW)
            ExternalClass.modify_parameter_value("DP_EDGEBAR_HEIGHT", inst_, gratingParameters("LOADBAR_HEIGHT"))
            ExternalClass.modify_parameter_value("DP_EDGEBAR_THICKNESS", inst_, gratingParameters("LOADBAR_THICKNESS"))

            If recessDR("CORNER") = 1 Then
                ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, gratingParameters("WIDTH") - squareW)
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, 0)

                'Position
                ExternalClass.modify_parameter_value("DP_V_EDGEBAR_POS", inst_, squareL)
                ExternalClass.modify_parameter_value("DP_V_EDGEBAR_BOTTOM", inst_, gratingParameters("LOADBAR_THICKNESS"))
                ExternalClass.modify_parameter_value("DP_H_EDGEBAR_POS", inst_, 0)

            ElseIf recessDR("CORNER") = 2 Then
                ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, gratingParameters("WIDTH") - squareW)
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, gratingParameters("LENGTH") - squareL - gratingParameters("LOADBAR_THICKNESS"))

                'Position
                ExternalClass.modify_parameter_value("DP_V_EDGEBAR_POS", inst_, 0)
                ExternalClass.modify_parameter_value("DP_V_EDGEBAR_BOTTOM", inst_, gratingParameters("LOADBAR_THICKNESS"))
                ExternalClass.modify_parameter_value("DP_H_EDGEBAR_POS", inst_, 0)

            ElseIf recessDR("CORNER") = 3 Then
                ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, 0)
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, gratingParameters("LENGTH") - squareL - gratingParameters("LOADBAR_THICKNESS"))

                'Position
                ExternalClass.modify_parameter_value("DP_V_EDGEBAR_POS", inst_, 0)
                ExternalClass.modify_parameter_value("DP_V_EDGEBAR_BOTTOM", inst_, 0)
                ExternalClass.modify_parameter_value("DP_H_EDGEBAR_POS", inst_, squareW - gratingParameters("LOADBAR_THICKNESS"))
            Else
                ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, 0)
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, 0)

                'Position
                ExternalClass.modify_parameter_value("DP_V_EDGEBAR_POS", inst_, squareL)
                ExternalClass.modify_parameter_value("DP_V_EDGEBAR_BOTTOM", inst_, 0)
                ExternalClass.modify_parameter_value("DP_H_EDGEBAR_POS", inst_, squareW - gratingParameters("LOADBAR_THICKNESS"))
            End If

        ElseIf recessDR("RECESS TYPE") = "ANGLE" Then
            inst_ = ExternalClass.instantiate_parts(parentID, "recess_angle", "")

            ExternalClass.modify_parameter_value("DP_EDGEBAR_HEIGHT", inst_, gratingParameters("LOADBAR_HEIGHT"))
            ExternalClass.modify_parameter_value("DP_EDGEBAR_THICKNESS", inst_, gratingParameters("LOADBAR_THICKNESS"))

            If recessDR("CORNER") = 1 Then
                ExternalClass.modify_parameter_value("DP_LENGTH", inst_, recessDR("WIDTH"))
                ExternalClass.modify_parameter_value("DP_WIDTH", inst_, recessDR("LENGTH"))
                ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, gratingParameters("WIDTH"))
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, 0)
                ExternalClass.modify_parameter_value("DP_ROTATION", inst_, 90)

            ElseIf recessDR("CORNER") = 2 Then
                ExternalClass.modify_parameter_value("DP_LENGTH", inst_, recessDR("LENGTH"))
                ExternalClass.modify_parameter_value("DP_WIDTH", inst_, recessDR("WIDTH"))
                ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, gratingParameters("WIDTH"))
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, gratingParameters("LENGTH"))
                ExternalClass.modify_parameter_value("DP_ROTATION", inst_, 180)

            ElseIf recessDR("CORNER") = 3 Then
                ExternalClass.modify_parameter_value("DP_LENGTH", inst_, recessDR("WIDTH"))
                ExternalClass.modify_parameter_value("DP_WIDTH", inst_, recessDR("LENGTH"))
                ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, 0)
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, gratingParameters("LENGTH"))
                ExternalClass.modify_parameter_value("DP_ROTATION", inst_, 270)

            Else
                ExternalClass.modify_parameter_value("DP_LENGTH", inst_, recessDR("LENGTH"))
                ExternalClass.modify_parameter_value("DP_WIDTH", inst_, recessDR("WIDTH"))
                ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, 0)
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, 0)
                ExternalClass.modify_parameter_value("DP_ROTATION", inst_, 0)

            End If

        ElseIf recessDR("RECESS TYPE") = "SIDE SQUARE" Then
            inst_ = ExternalClass.instantiate_parts(parentID, "recess_side_square", "")

            ExternalClass.modify_parameter_value("DP_EDGEBAR_HEIGHT", inst_, gratingParameters("LOADBAR_HEIGHT"))
            ExternalClass.modify_parameter_value("DP_EDGEBAR_THICKNESS", inst_, gratingParameters("LOADBAR_THICKNESS"))

            If recessDR("SIDE") = 1 Then
                ExternalClass.modify_parameter_value("DP_LENGTH", inst_, recessDR("WIDTH"))
                ExternalClass.modify_parameter_value("DP_WIDTH", inst_, recessDR("LENGTH"))
                ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, gratingParameters("WIDTH"))
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, recessDR("OFFSET"))
                ExternalClass.modify_parameter_value("DP_ROTATION", inst_, 90)

            ElseIf recessDR("SIDE") = 2 Then
                ExternalClass.modify_parameter_value("DP_LENGTH", inst_, recessDR("LENGTH"))
                ExternalClass.modify_parameter_value("DP_WIDTH", inst_, recessDR("WIDTH"))
                ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, gratingParameters("WIDTH") - recessDR("OFFSET"))
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, gratingParameters("LENGTH"))
                ExternalClass.modify_parameter_value("DP_ROTATION", inst_, 180)

            ElseIf recessDR("SIDE") = 3 Then
                ExternalClass.modify_parameter_value("DP_LENGTH", inst_, recessDR("WIDTH"))
                ExternalClass.modify_parameter_value("DP_WIDTH", inst_, recessDR("LENGTH"))
                ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, 0)
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, gratingParameters("LENGTH") - recessDR("OFFSET"))
                ExternalClass.modify_parameter_value("DP_ROTATION", inst_, 270)

            Else
                ExternalClass.modify_parameter_value("DP_LENGTH", inst_, recessDR("LENGTH"))
                ExternalClass.modify_parameter_value("DP_WIDTH", inst_, recessDR("WIDTH"))
                ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, recessDR("OFFSET"))
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, 0)
                ExternalClass.modify_parameter_value("DP_ROTATION", inst_, 0)

            End If
        End If


    End Sub

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

        Dim boolstatus As Boolean
        Dim AssemblyExtension As SldWorks.ModelDocExtension

        Dim Assembly = swApp.ActiveDoc
        Dim SelMgr = Assembly.SelectionManager()
        AssemblyExtension = Assembly.Extension
        Dim sldata = SelMgr.CreateSelectData

        Dim partDT As New DataTable
        partDT = partModel.selectPart("*")

        Assembly.ClearSelection2(True)

        ' Grating part
        Dim gratingPartName As String = partDT.Rows(0)("partName")
        boolstatus = AssemblyExtension.SelectByID2(gratingPartName & "-1@" & designName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
        Dim gratingPart = SelMgr.GetSelectedObject6(1, -1)

        Assembly.EditPart

        Dim components = Assembly.GetComponents(True)

        Dim indexGrating As Integer
        For i = 0 To components.Length - 1
            If components(i).Name2 = gratingPartName & "-1" Then
                indexGrating = i
                Exit For
            End If
        Next

        Dim gratingBodies = components(indexGrating).GetBodies2(0)

        Dim recessParts = partDT.Select("parentName = '" & gratingPartName & "'")
        Dim recessPartName As String
        For i = 0 To recessParts.Count - 1
            Assembly.ClearSelection2(True)

            ' Recess part
            recessPartName = recessParts(i)("partName")
            boolstatus = AssemblyExtension.SelectByID2(recessPartName & "-1@" & designName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
            Dim recessPart = SelMgr.GetSelectedObject6(1, -1)

            Dim indexRecess As Integer
            For j = 0 To components.Length - 1
                If components(j).Name2 = recessPartName & "-1" Then
                    indexRecess = j
                    Exit For
                End If
            Next

            Dim recessBodies = components(indexRecess).GetBodies2(0)

            For j = 0 To gratingBodies.Length - 1
                sldata.Mark = 1

                Dim bodyName As String = gratingBodies(j).Name & "@" & gratingPartName & "-1@" & designName
                boolstatus = AssemblyExtension.SelectByID2(bodyName, "SOLIDBODY", 0, 0, 0, True, 1, Nothing, 0)

                sldata.Mark = 4

                Dim indexBody As Integer
                For k = 0 To recessBodies.Length - 1
                    If recessBodies(k).Name = "Boss-Extrude1" Then
                        indexBody = k
                        Exit For
                    End If
                Next

                Dim recessFace = recessBodies(indexBody).GetFirstFace
                recessFace.Select4(True, sldata)

                Assembly.FeatureManager.InsertIndent(0.01, 0, False, True, True, False)
                Assembly.ClearSelection2(True)
            Next
        Next
    End Sub

End Class
