X2021/Wardrobe/SW_Drawing_Gratings.vb

144 lines
5.3 KiB
VB.net

Imports XCCLibrary
Imports SldWorks
Public Class SW_Drawing_Gratings
Public Shared Sub CreateDrawing()
Dim pointTable As New DataTable
pointTable = GUI_Gratings_Data.Create_PointTable()
Dim gratingHeight As Decimal = 0.025 ' Behövs variabel
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, gratingHeight, 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 = GUI.filepath & "\Temp" & "\TESTPART" & 1 & ".SLDPRT"
longstatus = iPart.SaveAs3(newName, 0, 0)
Model.ClearSelection2(True)
Create_Drawing(iPart)
End Sub
Private Shared Sub Create_Drawing(iModel As SldWorks.IModelDoc2)
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(GUI.filepath & "\Temp\TESTPART1.SLDPRT", "*Front", swSheetWidth / 2, swSheetHeight / 2, 0)
Dim swExtensions As SldWorks.ModelDocExtension
swExtensions = iDrawing.Extension
Dim RootComp = myView.RootDrawingComponent
Dim CompName = RootComp.Name
Dim dimension As IDisplayDimension
Dim OutLine = myView.GetOutline
Dim X_Mid = (OutLine(2) - OutLine(0)) / 2 + OutLine(0)
Dim Y_Mid = (OutLine(3) - OutLine(1)) / 2 + OutLine(1)
For i = 1 To 2
Dim point1Name As String = "Point" & i & "@Sketch1@" & CompName & "@" & myView.GetName2
Dim point2Name As String = "Point" & i + 1 & "@Sketch1@" & CompName & "@" & myView.GetName2
iDrawing.ClearSelection2(True)
swExtensions.SelectByID2(point1Name, "EXTSKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)
swExtensions.SelectByID2(point2Name, "EXTSKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)
If i = 1 Then
dimension = iDrawing.AddDimension2(X_Mid, OutLine(3), 0)
Else
dimension = iDrawing.AddDimension2(OutLine(2), Y_Mid, 0)
End If
iDrawing.ClearSelection2(True)
dimension.SetUnits2(False, 0, 1, 0, True, 12)
dimension.CenterText = True
dimension.SetPrecision3(0, 0, 0, 0)
Next
Dim myView2 As View
myView2 = iDrawing.CreateDrawViewFromModelView3(GUI.filepath & "\Temp\TESTPART1.SLDPRT", "*Top", swSheetWidth / 2, OutLine(3) + 0.03, 0)
Dim longstatus As Integer
Dim newName As String
newName = GUI.filepath & "\Temp" & "\TESTDRAWING" & 1 & ".SLDDRW"
longstatus = iDrawing.SaveAs3(newName, 0, 0)
End Sub
End Class