X2021/Wardrobe/SW_Drawing_Gratings.vb

245 lines
9.8 KiB
VB.net

Imports XCCLibrary
Imports SldWorks
Public Class SW_Drawing_Gratings
Private Shared sideCounter As Integer()
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
boolstatus = Model.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Model.FeatureManager.InsertRefPlane(8, gratingHeight, 0, 0, 0, 0)
'Rename sketch?
swSkMgr.InsertSketch(True)
Dim swModelDocExtension As ModelDocExtension
swModelDocExtension = Model.Extension
Dim status As Boolean
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)
sideCounter = {0, 0, 0, 0}
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(GUI.filepath & "\X2021\Weland ritningsmallar\A3 Part.slddrt")
swSheet.ReloadTemplate(True)
Dim myView As View
myView = iDrawing.CreateDrawViewFromModelView3(GUI.filepath & "\Temp\TESTPART1.SLDPRT", "*Front", swSheetWidth / 2, swSheetHeight / 2, 0)
'Dim viewScale = myView.ScaleDecimal()
Dim viewScale As Double
Dim lengthForDrawing As Integer = GUI_Gratings_Data.gratingMaxL
Dim widthForDrawing As Integer = GUI_Gratings_Data.gratingMaxW
If lengthForDrawing < widthForDrawing * 3 Then
'W styr
If widthForDrawing < 100 Then
viewScale = 1
ElseIf widthForDrawing < 200 Then
viewScale = 0.5
ElseIf widthForDrawing < 500 Then
viewScale = 0.2
Else
viewScale = 0.1
End If
Else
If lengthForDrawing < 300 Then
viewScale = 1
ElseIf lengthForDrawing < 600 Then
viewScale = 0.5
ElseIf lengthForDrawing < 1500 Then
viewScale = 0.2
ElseIf lengthForDrawing < 3000 Then
viewScale = 0.1
Else
viewScale = 0.05
End If
End If
myView.ScaleDecimal() = viewScale
Dim swExtensions As SldWorks.ModelDocExtension
swExtensions = iDrawing.Extension
Dim RootComp = myView.RootDrawingComponent
Dim CompName = RootComp.Name
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 = 2 To GUI_Functions.pointsMeasurements.Count - 1
Dim mesName As String = GUI_Functions.pointsMeasurements.Keys(i)
Add_Dimensions(GUI_Functions.pointsMeasurements(mesName), CompName, myView, iDrawing, swExtensions, OutLine, viewScale, X_Mid, Y_Mid)
Next
Add_Dimensions(GUI_Functions.pointsMeasurements("Lmes"), CompName, myView, iDrawing, swExtensions, OutLine, viewScale, X_Mid, Y_Mid)
Add_Dimensions(GUI_Functions.pointsMeasurements("Wmes"), CompName, myView, iDrawing, swExtensions, OutLine, viewScale, X_Mid, Y_Mid)
Dim myView2 As View
myView2 = iDrawing.CreateDrawViewFromModelView3(GUI.filepath & "\Temp\TESTPART1.SLDPRT", "*Bottom", swSheetWidth / 2, OutLine(3) + sideCounter(0) * 0.01 + 0.03, 0)
myView2.ScaleDecimal() = viewScale
Dim OutLine2 = myView2.GetOutline
Dim point1Name As String = "Front Plane@" & CompName & "@" & myView2.GetName2
Dim point2Name As String = "Plane1@" & CompName & "@" & myView2.GetName2
iDrawing.ClearSelection2(True)
Dim measurement As IDisplayDimension
swExtensions.SelectByID2(point1Name, "PLANE", 0, 0, 0, True, 0, Nothing, 0)
swExtensions.SelectByID2(point2Name, "PLANE", 0, 0, 0, True, 0, Nothing, 0)
measurement = iDrawing.AddVerticalDimension2(OutLine2(2) + 0.005, OutLine2(1), 0)
iDrawing.ClearSelection2(True)
measurement.SetUnits2(False, 0, 1, 0, True, 12)
measurement.CenterText = True
measurement.SetPrecision3(0, 0, 0, 0)
Dim longstatus As Integer
Dim newName As String
newName = GUI.filepath & "\Temp" & "\TESTDRAWING" & 1 & ".SLDDRW"
longstatus = iDrawing.SaveAs3(newName, 0, 0)
End Sub
Private Shared Sub Add_Dimensions(points() As Integer, CompName As String, myView As View, iDrawing As DrawingDoc, swExtensions As SldWorks.ModelDocExtension _
, OutLine() As Double, viewScale As Double, X_Mid As Double, Y_Mid As Double)
Dim measurement As IDisplayDimension
Dim point1NameSW As String = "Point" & points(0) & "@Sketch1@" & CompName & "@" & myView.GetName2
Dim point2NameSW As String = "Point" & points(1) & "@Sketch1@" & CompName & "@" & myView.GetName2
iDrawing.ClearSelection2(True)
swExtensions.SelectByID2(point1NameSW, "EXTSKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)
swExtensions.SelectByID2(point2NameSW, "EXTSKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)
Dim mesOffset = 0.01
If points(2) = 1 OrElse points(2) = 3 Then
Dim point1 As Decimal = GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(points(0) - 1))(2)
Dim point2 As Decimal = GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(points(1) - 1))(2)
Dim xPos As Decimal
If Math.Abs(point1) > Math.Abs(point2) Then
xPos = point2 + (point1 - point2) / 2
Else
xPos = point1 + (point2 - point1) / 2
End If
If points(2) = 1 Then
measurement = iDrawing.AddHorizontalDimension2(X_Mid + xPos * viewScale, OutLine(3) + 0.005 + mesOffset * sideCounter(0), 0)
Else
measurement = iDrawing.AddHorizontalDimension2(X_Mid + xPos * viewScale, OutLine(1) - 0.005 - mesOffset * sideCounter(2), 0)
End If
Else
Dim point1 As Decimal = GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(points(0) - 1))(3)
Dim point2 As Decimal = GUI_Drawing_Panel.points(GUI_Drawing_Panel.pointsOrder(points(1) - 1))(3)
Dim yPos As Decimal
If Math.Abs(point1) > Math.Abs(point2) Then
yPos = point2 + (point1 - point2) / 2
Else
yPos = point1 + (point2 - point1) / 2
End If
If points(2) = 2 Then
measurement = iDrawing.AddVerticalDimension2(OutLine(2) + 0.005 + mesOffset * sideCounter(1), Y_Mid + yPos * viewScale, 0)
Else
measurement = iDrawing.AddVerticalDimension2(OutLine(0) - 0.005 - mesOffset * sideCounter(3), Y_Mid + yPos * viewScale, 0)
End If
End If
sideCounter(points(2) - 1) = sideCounter(points(2) - 1) + 1
iDrawing.ClearSelection2(True)
'measurement.SetWitnessLineGap(1, True, 0.001) ' For break dimension line
measurement.SetUnits2(False, 0, 1, 0, True, 12)
measurement.CenterText = True
measurement.SetPrecision3(0, 0, 0, 0)
End Sub
End Class