Imports XCCLibrary
Imports SldWorks

Public Class Multiple_3D
    Private Shared gratingParameters As DataRow
    Private Shared designName As String
    Public Shared Sub BuildGrid()
        Dim pointTable As DataTable
        pointTable = Data.gratingPoints
        Dim pointTable1 As DataTable
        pointTable1 = Data.grossAreaPoints




        Dim exportTable As DataTable
        exportTable = User_Input.Create_ExportTable()

        XCCBaseClass.newDesign()
        ExternalClass.PopulateTamplateXMLGroup()

        Dim inst_ As Integer
        Dim parentID As Integer = 0
        gratingParameters = exportTable.Rows(0)

        For i = 0 To Grating_Fill.gratingDimensions.Rows.Count - 1
            inst_ = ExternalClass.instantiate_parts(parentID, gratingParameters("TYPE"), "") ' Ange pressure_welded eller type A, Serrated?
            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" Or gratingParameters("TYPE") = "pressure_welded_serrated" 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

            ExternalClass.modify_parameter_value("DP_GRATING_WIDTH", inst_, Grating_Fill.gratingDimensions.Rows(i)("WIDTH"))
            ExternalClass.modify_parameter_value("DP_GRATING_LENGTH", inst_, Grating_Fill.gratingDimensions.Rows(i)("LENGTH"))
            Dim offsetX As Integer = 0
            For j = 1 To Grating_Fill.gratingDimensions.Rows(i)("COLUMN")
                offsetX += Grating_Fill.gratingDimensions.Rows(i - j)("LENGTH")
            Next

            Dim offsetY As Integer = 0
            For j = 1 To Grating_Fill.gratingDimensions.Rows(i)("ROW")
                offsetY += Grating_Fill.gratingDimensions.Rows(i - Grating_Fill.numOfHorizontal * j)("WIDTH")
            Next

            ExternalClass.modify_parameter_value("DP_OFFSET_X", inst_, (Grating_Fill.gratingDimensions.Rows(i)("LENGTH") / 2) + offsetX)
            ExternalClass.modify_parameter_value("DP_OFFSET_Y", inst_, (Grating_Fill.gratingDimensions.Rows(i)("WIDTH") / 2) + offsetY)
        Next

        parentID = Grating_Fill.numOfHorizontal * (Grating_Fill.numOfVertical - 1) + 1
        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")

        Delete_Obsolete()
        Recess_Indent()

        Save_Assembly()
    End Sub


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

        Dim status As Boolean

        Dim Assebmly = swApp.ActiveDoc()
        status = Assebmly.SaveAs3(Settings.folderPaths("object_" & Data.objectNum & "_models3D_gratings_sw") & "\gratings_assembly.SLDASM", 0, 4)
    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"))
            ExternalClass.modify_parameter_value("DP_BOX_HEIGHT", inst_, gratingParameters("LOADBAR_HEIGHT"))


            If recessDR("CORNER") = 1 Then
                ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, Data.gratingW - 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_, Data.gratingW - squareW)
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, Data.gratingL - 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_, Data.gratingL - 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_, Data.gratingW)
                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_, Data.gratingW)
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, Data.gratingL)
                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_, Data.gratingL)
                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_, Data.gratingW)
                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_, Data.gratingW - recessDR("OFFSET"))
                ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, Data.gratingL)
                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_, Data.gratingL - 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

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

            ExternalClass.modify_parameter_value("DP_LENGTH", inst_, recessDR("LENGTH"))
            ExternalClass.modify_parameter_value("DP_WIDTH", inst_, recessDR("WIDTH"))
            ExternalClass.modify_parameter_value("DP_EDGEBAR_HEIGHT", inst_, gratingParameters("LOADBAR_HEIGHT"))
            ExternalClass.modify_parameter_value("DP_EDGEBAR_THICKNESS", inst_, gratingParameters("LOADBAR_THICKNESS"))
            ExternalClass.modify_parameter_value("DP_OFFSET_W", inst_, recessDR("OFFSET Y"))
            ExternalClass.modify_parameter_value("DP_OFFSET_L", inst_, recessDR("OFFSET X"))
        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 modelDoc As IModelDoc2
        modelDoc = Assembly

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

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

        Dim gratingParts = partDT.Select("parentName = 'START'")

        Dim components = Assembly.GetComponents(True)

        'Indent
        Dim recessParentName As String = partDT.Rows(Grating_Fill.numOfHorizontal * (Grating_Fill.numOfVertical - 1))("partName")
        Dim recessParts = partDT.Select("parentName = '" & recessParentName & "'")
        For i = 0 To recessParts.Count - 1
            Dim recessPartName As String = recessParts(i)("partName")
            If recessPartName.Split("_")(0) = "RECESS" Then

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

                Dim recessBodies = components(indexRecess).GetBodies2(0)
                Dim recessComponent As SldWorks.Component2 = components(indexRecess)

                Dim indexBody As Integer
                For l = 0 To recessBodies.Length - 1
                    If recessBodies(l).Name = "Cut-Body" Then
                        indexBody = l
                        Exit For
                    End If
                Next

                Dim recessFace = recessBodies(indexBody).GetFirstFace

                For j = 0 To Grating_Fill.numOfVertical * Grating_Fill.numOfHorizontal - 1
                    Assembly.ClearSelection2(True)

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

                        Assembly.EditPart

                        boolstatus = AssemblyExtension.SelectByID2("HorizontalSplitBody@" & gratingPartName & "-1@" & designName, "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
                        modelDoc.EditSuppress2()
                        boolstatus = AssemblyExtension.SelectByID2("VerticalSplitBody@" & gratingPartName & "-1@" & designName, "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
                        modelDoc.EditSuppress2()

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

                        Dim gratingComponent As SldWorks.Component2 = components(indexGrating)

                        Dim bodiesToCut = Check_Interference(gratingComponent, recessComponent, gratingPartName & "-1")
                        For k = 0 To bodiesToCut.Length - 1
                            Assembly.ClearSelection2(True)

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

                            sldata.Mark = 4
                            recessFace.Select4(True, sldata)

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

                        bodiesToCut = Check_Interference(gratingComponent, recessComponent, gratingPartName & "-1")
                        For k = 0 To bodiesToCut.Length - 1
                            boolstatus = AssemblyExtension.SelectByID2(bodiesToCut(k).Name & "@" & gratingPartName & "-1@" & designName, "SOLIDBODY", 0, 0, 0, True, 0, Nothing, 0)
                            Dim myFeature As Object
                            Dim modelFeat As FeatureManager = modelDoc.FeatureManager
                            myFeature = modelFeat.InsertDeleteBody2(False)
                            Dim a = 1
                        Next

                        boolstatus = AssemblyExtension.SelectByID2("HorizontalSplitBody@" & gratingPartName & "-1@" & designName, "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
                        modelDoc.EditUnsuppress2()
                        boolstatus = AssemblyExtension.SelectByID2("VerticalSplitBody@" & gratingPartName & "-1@" & designName, "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
                        modelDoc.EditUnsuppress2()
                        Assembly.EditAssembly
                    End If
                Next

                Assembly.ClearSelection2(True)

                boolstatus = AssemblyExtension.SelectByID2(recessPartName & "-1@" & designName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
                Dim recessPart2 = SelMgr.GetSelectedObject6(1, -1)

                Assembly.EditPart

                Assembly.ClearSelection2(True)
                boolstatus = AssemblyExtension.SelectByID2("Interference-Body@" & recessPartName & "-1@" & designName, "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
                modelDoc.EditSuppress2()
                boolstatus = AssemblyExtension.SelectByID2("Cut-Body@" & recessPartName & "-1@" & designName, "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
                modelDoc.EditSuppress2()

                For j = 0 To Grating_Fill.numOfVertical * Grating_Fill.numOfHorizontal - 1
                    Dim gratingPartName As String = partDT.Rows(j)("partName")
                    Dim indexGrating As Integer
                    For l = 0 To components.Length - 1
                        If components(l).Name2 = gratingPartName & "-1" Then
                            indexGrating = l
                            Exit For
                        End If
                    Next
                    Dim gratingComponent As SldWorks.Component2 = components(indexGrating)

                    Assembly.ClearSelection2(True)
                    Dim bodiesToCut = Check_Interference(recessComponent, gratingComponent, recessPartName & "-1")

                    If bodiesToCut.Length > 0 Then
                        For k = 1 To 2
                            Dim splitBodyName As String
                            If k = 1 Then
                                splitBodyName = "VerticalSplitBody"
                            Else
                                splitBodyName = "HorizontalSplitBody"
                            End If

                            Dim gratingBodies = components(indexGrating).GetBodies2(0)
                            Dim cutBodyIndex As Integer
                            For l = 0 To gratingBodies.Length - 1
                                If gratingBodies(l).Name = splitBodyName Then
                                    cutBodyIndex = l
                                    Exit For
                                End If
                            Next
                            Dim cutBody = gratingBodies(cutBodyIndex)

                            'recessBodies = components(indexRecess).GetBodies2(0)
                            For l = 0 To bodiesToCut.Length - 1
                                ' If recessBodies(l).Name <> "Cut-Body" AndAlso recessBodies(l).Name <> "Interference-Body" Then

                                Dim recessBody = bodiesToCut(l)

                                Assembly.ClearSelection2(True)

                                sldata.Mark = 1
                                Dim bodyName As String = bodiesToCut(l).Name & "@" & recessPartName & "-1@" & designName
                                boolstatus = AssemblyExtension.SelectByID2(bodyName, "SOLIDBODY", 0, 0, 0, True, 1, Nothing, 0)

                                sldata.Mark = 4
                                Dim cutFace = cutBody.GetFirstFace
                                cutFace.Select4(True, sldata)

                                Assembly.FeatureManager.InsertIndent(0.01, 0, False, True, True, False)

                                'End If
                            Next
                        Next
                    End If
                    Assembly.ClearSelection2(True)
                Next

                boolstatus = AssemblyExtension.SelectByID2("Interference-Body@" & recessPartName & "-1@" & designName, "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
                modelDoc.EditUnsuppress2()
                boolstatus = AssemblyExtension.SelectByID2("Cut-Body@" & recessPartName & "-1@" & designName, "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
                modelDoc.EditUnsuppress2()

            End If
            Assembly.EditAssembly
            boolstatus = modelDoc.EditRebuild3()
        Next

        If gratingParameters("TYPE") = "pressure_welded_serrated" Or gratingParameters("TYPE") = "type_a_serrated" Then
            Dim numOfFeat As Integer = 0
            If gratingParameters("TYPE") = "pressure_welded_serrated" Then
                numOfFeat = 5
            ElseIf gratingParameters("TYPE") = "type_a_serrated" Then
                numOfFeat = 2
            End If
            For i = 0 To Grating_Fill.numOfVertical * Grating_Fill.numOfHorizontal - 1
                Dim gratingPartName As String = partDT.Rows(i)("partName")
                Dim indexGrating As Integer
                For j = 0 To components.Length - 1
                    If components(j).Name2 = gratingPartName & "-1" Then
                        indexGrating = j
                        Exit For
                    End If
                Next
                Dim gratingComponent As SldWorks.Component2 = components(indexGrating)
                boolstatus = AssemblyExtension.SelectByID2(gratingPartName & "-1@" & designName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
                Assembly.EditPart

                For j = 1 To numOfFeat
                    boolstatus = AssemblyExtension.SelectByID2("Serrated-Cut" & j & "@" & gratingPartName & "-1@" & designName, "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
                    modelDoc.EditUnsuppress2()
                Next
                Assembly.EditAssembly
            Next
        End If

    End Sub


    Private Shared Sub Delete_Obsolete()
        Dim listOfObsolete As List(Of Integer)
        listOfObsolete = Find_Obsolete()

        Dim swApp As SldWorks.SldWorks
        swApp = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application"), SldWorks.SldWorks)
        Dim boolstatus As Boolean
        Dim Assembly = swApp.ActiveDoc
        Dim AssemblyExtension As SldWorks.ModelDocExtension
        AssemblyExtension = Assembly.Extension

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

        Dim gratingParts = partDT.Select("parentName = 'START'")

        For i = 0 To listOfObsolete.Count - 1
            Dim gratingPartName As String = gratingParts(listOfObsolete(i))("partName")

            Assembly.ClearSelection2(True)
            boolstatus = AssemblyExtension.SelectByID2(gratingPartName & "-1@" & designName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
            Assembly.EditDelete
        Next
    End Sub

    Public Shared Function Find_Obsolete()
        Dim listOfObsolete As New List(Of Integer)

        For i = 0 To Grating_Fill.numOfVertical * Grating_Fill.numOfHorizontal - 1
            Dim gratingDR As DataRow = Grating_Fill.gratingDimensions.Rows(i)
            Dim origo(2) As Integer
            origo(0) = Data.gratingL / 2
            origo(1) = Data.gratingW / 2

            Dim GP As New List(Of Integer())
            Dim p1(1), p2(1), p3(1), p4(1) As Integer
            p1(0) = -origo(0)
            p2(0) = -origo(0) + gratingDR("LENGTH")
            For j = 1 To gratingDR("COLUMN")
                p1(0) += Grating_Fill.gratingDimensions.Rows(i - j)("LENGTH")
                p2(0) += Grating_Fill.gratingDimensions.Rows(i - j)("LENGTH")
            Next
            p1(1) = origo(1)
            p3(1) = origo(1) - gratingDR("WIDTH")
            For j = 1 To gratingDR("ROW")
                p1(1) -= Grating_Fill.gratingDimensions.Rows(i - Grating_Fill.numOfHorizontal * j)("WIDTH")
                p3(1) -= Grating_Fill.gratingDimensions.Rows(i - Grating_Fill.numOfHorizontal * j)("WIDTH")
            Next

            p2(1) = p1(1)
            p3(0) = p1(0)
            p4(0) = p2(0)
            p4(1) = p3(1)

            GP.Add(p1)
            GP.Add(p2)
            GP.Add(p3)
            GP.Add(p4)

            Dim cornerAngleCounter As Integer = 1
            Dim cornerRectangleCounter As Integer = 1
            Dim numOfSideRectangles As Integer = 0
            Dim numOfMiddleRectangles As Integer = 0
            For j = 0 To Data.recessData.Rows.Count - 1
                If Data.recessData.Rows(j)("RECESS TYPE") = "ANGLE" Then
                    Dim RP As New List(Of Integer())
                    Dim rec1(1), rec2(1), rec3(1) As Integer

                    rec1(0) = Data.grossAreaPoints.Rows(Data.recessData.Rows(j)("CORNER") - 1)("X") * 1000
                    rec1(1) = Data.grossAreaPoints.Rows(Data.recessData.Rows(j)("CORNER") - 1)("Y") * 1000

                    Dim rowIndex As Integer
                    For k = 0 To Data.gratingPoints.Rows.Count - 1
                        If Data.gratingPoints.Rows(k)("NAME") = "CA" & cornerAngleCounter & "_" & 1 Then
                            rowIndex = k
                        End If
                    Next

                    rec2(0) = Data.gratingPoints.Rows(rowIndex)("X") * 1000
                    rec2(1) = Data.gratingPoints.Rows(rowIndex)("Y") * 1000

                    rec3(0) = Data.gratingPoints.Rows(rowIndex + 1)("X") * 1000
                    rec3(1) = Data.gratingPoints.Rows(rowIndex + 1)("Y") * 1000

                    RP.Add(rec1)
                    RP.Add(rec2)
                    RP.Add(rec3)

                    Dim gratingObsolete As Boolean = True
                    For k = 0 To 3
                        Dim pInTriangle As Boolean = Calculate_Triangle_Bool(GP(k), RP(0), RP(1), RP(2))
                        If pInTriangle = False Then
                            gratingObsolete = False
                            Exit For
                        End If
                    Next
                    If gratingObsolete = True Then
                        listOfObsolete.Add(i)
                    End If

                    cornerAngleCounter += 1

                ElseIf Data.recessData.Rows(j)("RECESS TYPE") = "SQUARE" OrElse Data.recessData.Rows(j)("RECESS TYPE") = "SIDE SQUARE" _
                        OrElse Data.recessData.Rows(j)("RECESS TYPE") = "MIDDLE SQUARE" Then
                    Dim RP As New List(Of Integer())
                    Dim rec1(1), rec2(1), rec3(1), rec4(1) As Integer

                    Dim rowIndex As Integer
                    If Data.recessData.Rows(j)("RECESS TYPE") = "SQUARE" Then
                        For k = 0 To Data.gratingPoints.Rows.Count - 1
                            If Data.gratingPoints.Rows(k)("NAME") = "CR" & cornerRectangleCounter & "_" & 1 Then
                                rowIndex = k
                            End If
                        Next
                        cornerRectangleCounter += 1
                    ElseIf Data.recessData.Rows(j)("RECESS TYPE") = "SIDE SQUARE" Then
                        For k = 0 To Data.gratingPoints.Rows.Count - 1
                            If Data.gratingPoints.Rows(k)("NAME") = "CS" & numOfSideRectangles & "_" & 1 Then
                                rowIndex = k
                            End If
                        Next
                        numOfSideRectangles += 1
                    End If

                    If Data.recessData.Rows(j)("RECESS TYPE") = "SQUARE" OrElse Data.recessData.Rows(j)("RECESS TYPE") = "SIDE SQUARE" Then
                        rec1(0) = Data.gratingPoints.Rows(rowIndex)("X") * 1000
                        rec1(1) = Data.gratingPoints.Rows(rowIndex)("Y") * 1000

                        rec2(0) = Data.gratingPoints.Rows(rowIndex + 1)("X") * 1000
                        rec2(1) = Data.gratingPoints.Rows(rowIndex + 1)("Y") * 1000

                        rec3(0) = Data.gratingPoints.Rows(rowIndex + 2)("X") * 1000
                        rec3(1) = Data.gratingPoints.Rows(rowIndex + 2)("Y") * 1000

                        If Data.recessData.Rows(j)("RECESS TYPE") = "SQUARE" Then
                            rec4(0) = Data.grossAreaPoints.Rows(Data.recessData.Rows(j)("CORNER") - 1)("X") * 1000
                            rec4(1) = Data.grossAreaPoints.Rows(Data.recessData.Rows(j)("CORNER") - 1)("Y") * 1000
                        ElseIf Data.recessData.Rows(j)("RECESS TYPE") = "SIDE SQUARE" Then
                            rec4(0) = Data.gratingPoints.Rows(rowIndex + 3)("X") * 1000
                            rec4(1) = Data.gratingPoints.Rows(rowIndex + 3)("Y") * 1000

                        End If

                    ElseIf Data.recessData.Rows(j)("RECESS TYPE") = "MIDDLE SQUARE" Then
                        numOfMiddleRectangles += 1

                        rec1(0) = Data.gratingMiddlePoints.Rows(numOfMiddleRectangles * 4 - 4)("X") * 1000
                        rec1(1) = Data.gratingMiddlePoints.Rows(numOfMiddleRectangles * 4 - 4)("Y") * 1000

                        rec2(0) = Data.gratingMiddlePoints.Rows(numOfMiddleRectangles * 4 - 3)("X") * 1000
                        rec2(1) = Data.gratingMiddlePoints.Rows(numOfMiddleRectangles * 4 - 3)("Y") * 1000

                        rec3(0) = Data.gratingMiddlePoints.Rows(numOfMiddleRectangles * 4 - 2)("X") * 1000
                        rec3(1) = Data.gratingMiddlePoints.Rows(numOfMiddleRectangles * 4 - 2)("Y") * 1000

                        rec4(0) = Data.gratingMiddlePoints.Rows(numOfMiddleRectangles * 4 - 1)("X") * 1000
                        rec4(1) = Data.gratingMiddlePoints.Rows(numOfMiddleRectangles * 4 - 1)("Y") * 1000
                    End If

                    RP.Add(rec1)
                    RP.Add(rec2)
                    RP.Add(rec3)
                    RP.Add(rec4)

                    Dim gratingObsolete As Boolean = True
                    For k = 0 To 3
                        Dim pInSquare As Boolean = Calculate_Square_Bool(GP(k), RP(0), RP(1), RP(2), RP(3))
                        If pInSquare = False Then
                            gratingObsolete = False
                            Exit For
                        End If
                    Next
                    If gratingObsolete = True Then
                        listOfObsolete.Add(i)
                    End If

                End If

            Next

        Next
        Return listOfObsolete
    End Function

    Private Shared Function Calculate_Triangle_Bool(gp As Integer(), p0 As Integer(), p1 As Integer(), p2 As Integer())

        Dim s As Double = p0(1) * p2(0) - p0(0) * p2(1) + (p2(1) - p0(1)) * gp(0) + (p0(0) - p2(0)) * gp(1)
        Dim t As Double = p0(0) * p1(1) - p0(1) * p1(0) + (p0(1) - p1(1)) * gp(0) + (p1(0) - p0(0)) * gp(1)

        If (s < 0 AndAlso t >= 0) OrElse (s >= 0 AndAlso t < 0) Then
            Return False
        End If

        Dim area As Double = -p1(1) * p2(0) + p0(1) * (p2(0) - p1(0)) + p0(0) * (p1(1) - p2(1)) + p1(0) * p2(1)

        If area < 0 Then
            If s <= 0 AndAlso s + t >= area Then
                Return True
            Else
                Return False
            End If
        Else
            If s >= 0 AndAlso s + t <= area Then
                Return True
            Else
                Return False
            End If
        End If
    End Function

    Private Shared Function Calculate_Square_Bool(gp As Integer(), p0 As Integer(), p1 As Integer(), p2 As Integer(), p3 As Integer())
        Dim squareArea As Double = Math.Abs(p0(0) - p1(0)) * Math.Abs(p1(1) - p2(1))
        If squareArea = 0 Then
            squareArea = Math.Abs(p0(1) - p1(1)) * Math.Abs(p1(0) - p2(0))
        End If

        Dim area1 As Double = Math.Abs(gp(0) * p0(1) + gp(1) * p1(0) + p0(0) * p1(1) - p0(1) * p1(0) - gp(1) * p0(0) - gp(0) * p1(1)) / 2
        Dim area2 As Double = Math.Abs(gp(0) * p1(1) + gp(1) * p2(0) + p1(0) * p2(1) - p1(1) * p2(0) - gp(1) * p1(0) - gp(0) * p2(1)) / 2
        Dim area3 As Double = Math.Abs(gp(0) * p2(1) + gp(1) * p3(0) + p2(0) * p3(1) - p2(1) * p3(0) - gp(1) * p2(0) - gp(0) * p3(1)) / 2
        Dim area4 As Double = Math.Abs(gp(0) * p3(1) + gp(1) * p0(0) + p3(0) * p0(1) - p3(1) * p0(0) - gp(1) * p3(0) - gp(0) * p0(1)) / 2
        Dim compArea As Double = area1 + area2 + area3 + area4

        If compArea > squareArea Then
            Return False
        Else
            Return True
        End If
    End Function

    Private Shared Function Check_Interference(comp1 As SldWorks.Component2, comp2 As SldWorks.Component2, comp1Name As String)

        Dim swApp As SldWorks.SldWorks
        Dim swModel As SldWorks.ModelDoc2
        Dim swAssy As SldWorks.AssemblyDoc
        Dim CompArray(1) As SldWorks.Component2
        Dim vCompArray
        Dim vIntCompArray
        Dim vIntFaceArray

        Dim interfering

        swApp = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application"), SldWorks.SldWorks)
        swModel = swApp.ActiveDoc
        swAssy = swModel

        CompArray(0) = comp1
        CompArray(1) = comp2

        vCompArray = CompArray
        swAssy.ToolsCheckInterference2(2, (vCompArray), interfering, vIntCompArray, vIntFaceArray)

        Dim Bodies(UBound(vIntFaceArray)) As Object
        Dim BodyNames(UBound(vIntFaceArray)) As String
        Dim CompNames(UBound(vIntFaceArray)) As String

        For i = 0 To UBound(vIntFaceArray)

            Bodies(i) = vIntFaceArray(i).GetBody
            BodyNames(i) = Bodies(i).Name
            CompNames(i) = vIntCompArray(i).Name2

        Next

        Dim bodiesToCutList As New List(Of Object)

        If CompNames(0) = comp1Name Then
            bodiesToCutList.Add(Bodies(0))
        End If
        For i = 1 To CompNames.Length - 1
            If CompNames(i) <> comp1Name Then
                Exit For
            ElseIf BodyNames(i) <> BodyNames(i - 1) Then
                bodiesToCutList.Add(Bodies(i))
            End If
        Next

        Dim bodiesToCut() As Object
        bodiesToCut = bodiesToCutList.ToArray()

        Return bodiesToCut
    End Function
End Class
