SWDrawingStandardView Constant
SWDrawingStandardView Constant
(OP)
I am playing with a VB program that I downloaded through the API help, but I have run into a problem. First off, the program opens a SW assembly, creates a drawing from the assembly. It then cycles through each view to determine the size and rescales it to fit the border that was picked. It will then finish by adding all the details the drawing. Its seems like a very slick program when it works. Now to the error.
When I run the program (see code below), it errors out. I tracked down the problem to an IF statement that is never activated, leaving variable undefined. The original program was written to look for a ViewType of 6 (should match up to SWDrawingStandardView), but a value of 6 is never seen. The first value it sees (Drawing View 1) is Type 7 (SWDrawing NamedView = 7). If I change the IF statement to look for a ViewType of 7, the program works.
Why doesn't it recognize the first drawing view as a standard view?
Here is the code (just for this section): Thanks, Joe
Sub CenterViews()
Dim Part As SldWorks.ModelDoc2
Dim View As SldWorks.View
Dim Sheet As SldWorks.Sheet
Dim Name As String
Dim Position As Variant
Dim PositionMainView As Variant
Dim MainView As SldWorks.View
Dim ViewType As Long
Dim DrScale As Double
Dim SheetBoundBox As Variant
Dim CurViewBoundBox As Variant
Dim ViewsBoundBox As Variant
Dim FirstView As Boolean
Dim TitleBlockHgt As Double
Dim RevBlockHgt As Double
Dim ViewZoneCenX As Double
Dim ViewZoneCenY As Double
Dim ViewsCenX As Double
Dim ViewsCenY As Double
Dim DeltaX As Double
Dim DeltaY As Double
Dim TemplateBlockHgts As Variant
Dim MarginY As Double
Dim ViewsLargerBy As Double
Dim LocY() As Double
Dim ViewNo As Long
Set Part = swApp.ActiveDoc
'Get the active Drawing Sheet
Set View = Part.GetFirstView()
SheetBoundBox = View.GetOutline()
MarginY = 0.0254 'An extra bit of space
TemplateBlockHgts = FindTemplateBlockHgts
TitleBlockHgt = TemplateBlockHgts(0)
RevBlockHgt = TemplateBlockHgts(1)
Part.EditSheet
FirstView = True
ViewNo = 0
Set View = View.GetNextView
Do While Not View Is Nothing
Name = View.Name
'Store the y position of each view for later adjustments
'Later on as the main view is adjusted the dependent views will move
'along with it - we want to locate them relative to their original positions
Position = Empty
Position = View.Position
ReDim Preserve LocY(ViewNo) As Double
LocY(ViewNo) = Position(1)
ViewType = View.Type
CurViewBoundBox = View.GetOutline()
'Keep track of the bounding box for all the views combined
If FirstView Then
ViewsBoundBox = CurViewBoundBox
FirstView = False
Else
ViewsBoundBox(0) = IIf(CurViewBoundBox(0) < ViewsBoundBox(0), CurViewBoundBox(0), ViewsBoundBox(0))
ViewsBoundBox(1) = IIf(CurViewBoundBox(1) < ViewsBoundBox(1), CurViewBoundBox(1), ViewsBoundBox(1))
ViewsBoundBox(2) = IIf(CurViewBoundBox(2) > ViewsBoundBox(2), CurViewBoundBox(2), ViewsBoundBox(2))
ViewsBoundBox(3) = IIf(CurViewBoundBox(3) > ViewsBoundBox(3), CurViewBoundBox(3), ViewsBoundBox(3))
End If
If swDrawingStandardView = ViewType Then
Set MainView = View
End If
Set View = View.GetNextView
ViewNo = ViewNo + 1
Loop
'Find the center of the sheet's usable area
ViewZoneCenX = (SheetBoundBox(2) - SheetBoundBox(0)) / 2
ViewZoneCenY = (RevBlockHgt - TitleBlockHgt) / 2 + TitleBlockHgt
'Find the center of the combined views' bounding box
ViewsCenX = (ViewsBoundBox(2) - ViewsBoundBox(0)) / 2 + ViewsBoundBox(0)
ViewsCenY = (ViewsBoundBox(3) - ViewsBoundBox(1)) / 2 + ViewsBoundBox(1)
'Find how much the views need to be moved to center them
DeltaX = ViewZoneCenX - ViewsCenX
DeltaY = ViewZoneCenY - ViewsCenY
'Move the main view to the center of the sheet's usable area
Position = MainView.Position
Position(0) = Position(0) + DeltaX
Position(1) = Position(1) + DeltaY
MainView.Position = Position
'Amount by which the views take up more space
'than is available between the rev and title blocks
ViewsLargerBy = (ViewsBoundBox(3) - ViewsBoundBox(1)) - (RevBlockHgt - TitleBlockHgt)
'If views need to be positioned
If ((ViewsLargerBy + MarginY + MarginY) > 0) Then
Set View = Part.GetFirstView() 'The sheet
Set View = View.GetNextView 'Skip to the first view
ViewNo = 0
Do While Not View Is Nothing
Position = View.Position()
If (Position(1) > ViewZoneCenY) Then 'This is an upper view so move it down
Position(1) = LocY(ViewNo) + DeltaY + (ViewsLargerBy / 2#) - MarginY
View.Position = Position
Else 'This is a lower view so move it up
Position(1) = LocY(ViewNo) + DeltaY - (ViewsLargerBy / 2#) + MarginY
View.Position = Position
End If
Set View = View.GetNextView
ViewNo = ViewNo + 1
Loop
End If
Part.EditRebuild3
End Sub
When I run the program (see code below), it errors out. I tracked down the problem to an IF statement that is never activated, leaving variable undefined. The original program was written to look for a ViewType of 6 (should match up to SWDrawingStandardView), but a value of 6 is never seen. The first value it sees (Drawing View 1) is Type 7 (SWDrawing NamedView = 7). If I change the IF statement to look for a ViewType of 7, the program works.
Why doesn't it recognize the first drawing view as a standard view?
Here is the code (just for this section): Thanks, Joe
Sub CenterViews()
Dim Part As SldWorks.ModelDoc2
Dim View As SldWorks.View
Dim Sheet As SldWorks.Sheet
Dim Name As String
Dim Position As Variant
Dim PositionMainView As Variant
Dim MainView As SldWorks.View
Dim ViewType As Long
Dim DrScale As Double
Dim SheetBoundBox As Variant
Dim CurViewBoundBox As Variant
Dim ViewsBoundBox As Variant
Dim FirstView As Boolean
Dim TitleBlockHgt As Double
Dim RevBlockHgt As Double
Dim ViewZoneCenX As Double
Dim ViewZoneCenY As Double
Dim ViewsCenX As Double
Dim ViewsCenY As Double
Dim DeltaX As Double
Dim DeltaY As Double
Dim TemplateBlockHgts As Variant
Dim MarginY As Double
Dim ViewsLargerBy As Double
Dim LocY() As Double
Dim ViewNo As Long
Set Part = swApp.ActiveDoc
'Get the active Drawing Sheet
Set View = Part.GetFirstView()
SheetBoundBox = View.GetOutline()
MarginY = 0.0254 'An extra bit of space
TemplateBlockHgts = FindTemplateBlockHgts
TitleBlockHgt = TemplateBlockHgts(0)
RevBlockHgt = TemplateBlockHgts(1)
Part.EditSheet
FirstView = True
ViewNo = 0
Set View = View.GetNextView
Do While Not View Is Nothing
Name = View.Name
'Store the y position of each view for later adjustments
'Later on as the main view is adjusted the dependent views will move
'along with it - we want to locate them relative to their original positions
Position = Empty
Position = View.Position
ReDim Preserve LocY(ViewNo) As Double
LocY(ViewNo) = Position(1)
ViewType = View.Type
CurViewBoundBox = View.GetOutline()
'Keep track of the bounding box for all the views combined
If FirstView Then
ViewsBoundBox = CurViewBoundBox
FirstView = False
Else
ViewsBoundBox(0) = IIf(CurViewBoundBox(0) < ViewsBoundBox(0), CurViewBoundBox(0), ViewsBoundBox(0))
ViewsBoundBox(1) = IIf(CurViewBoundBox(1) < ViewsBoundBox(1), CurViewBoundBox(1), ViewsBoundBox(1))
ViewsBoundBox(2) = IIf(CurViewBoundBox(2) > ViewsBoundBox(2), CurViewBoundBox(2), ViewsBoundBox(2))
ViewsBoundBox(3) = IIf(CurViewBoundBox(3) > ViewsBoundBox(3), CurViewBoundBox(3), ViewsBoundBox(3))
End If
If swDrawingStandardView = ViewType Then
Set MainView = View
End If
Set View = View.GetNextView
ViewNo = ViewNo + 1
Loop
'Find the center of the sheet's usable area
ViewZoneCenX = (SheetBoundBox(2) - SheetBoundBox(0)) / 2
ViewZoneCenY = (RevBlockHgt - TitleBlockHgt) / 2 + TitleBlockHgt
'Find the center of the combined views' bounding box
ViewsCenX = (ViewsBoundBox(2) - ViewsBoundBox(0)) / 2 + ViewsBoundBox(0)
ViewsCenY = (ViewsBoundBox(3) - ViewsBoundBox(1)) / 2 + ViewsBoundBox(1)
'Find how much the views need to be moved to center them
DeltaX = ViewZoneCenX - ViewsCenX
DeltaY = ViewZoneCenY - ViewsCenY
'Move the main view to the center of the sheet's usable area
Position = MainView.Position
Position(0) = Position(0) + DeltaX
Position(1) = Position(1) + DeltaY
MainView.Position = Position
'Amount by which the views take up more space
'than is available between the rev and title blocks
ViewsLargerBy = (ViewsBoundBox(3) - ViewsBoundBox(1)) - (RevBlockHgt - TitleBlockHgt)
'If views need to be positioned
If ((ViewsLargerBy + MarginY + MarginY) > 0) Then
Set View = Part.GetFirstView() 'The sheet
Set View = View.GetNextView 'Skip to the first view
ViewNo = 0
Do While Not View Is Nothing
Position = View.Position()
If (Position(1) > ViewZoneCenY) Then 'This is an upper view so move it down
Position(1) = LocY(ViewNo) + DeltaY + (ViewsLargerBy / 2#) - MarginY
View.Position = Position
Else 'This is a lower view so move it up
Position(1) = LocY(ViewNo) + DeltaY - (ViewsLargerBy / 2#) + MarginY
View.Position = Position
End If
Set View = View.GetNextView
ViewNo = ViewNo + 1
Loop
End If
Part.EditRebuild3
End Sub






RE: SWDrawingStandardView Constant
If swDrawingStandardView = ViewType Then
to
If (swDrawingStandardView = ViewType) OR (swDrawingNamedView = ViewType) Then
You won't lose the original functionality and, if it's really working fine with swDrawingNamedView, I'd say don't worry about it.
There may have been a change somewhere in SW behavior between the time the API example was written and whatever SW version you're running. What code is used to create the views?