4 Jul, 2019

## Scaling Drawing Views

### Scope

We often produce 2D documentation of parts varying in size; trying to fit them all on a single-sheet size per project deliverable/package, and we are aware that CATIA drawing has many inefficiencies, one of the most common bottlenecks found is scaling views to fit. Today I will provide an automation example to mitigate scaling views manually. But first, I suggest taking a look at this post, which provides an overview of the data model used for CATIA drawings.

### Model file structure

The example consist of a Physical Product (Part) containing a Shape representation of a metal angle that is controlled by a Length parameter. The part also contains two other representations: a Drawing to host views, and a Knowledge Engineering Specifications (KES) to hold our automation, which will be in VBScript. One of the advantages of storing automation objects in KES representations is that they can be reused in different assemblies independently from other representations.

### Solution

In todays script, I show two methods to set the scale value for the drawing views:
1- Fit scale, we will apply an arbitrary scale ratio to fit the part view within a given sheet-size.
2- Predefined scales; we will apply a value scale from a pre-populated list of scales. Below a diagram showing the logic of the range selection.

Below is the code used in the above example:

' Browsing the model, to get the root of the assembly, we need to call a PLMProductServic.
' This service provides us with method to get more information about the model.
' for example the root Occurrence.
Set oEdit = CATIA.ActiveEditor
Dim pSer As PLMProductService
Set pSer = oEdit.GetService("PLMProductService")

Dim rootOcc As VPMRootOccurrence
Set rootOcc = pSer.RootOccurrence

Dim RefonRoot As VPMReference
Set RefonRoot = rootOcc.ReferenceRootOccurrenceOf

Dim RepInst As VPMRepInstance
Set RepInst = RefonRoot.RepInstances.Item(1)
Dim RepRef As VPMRepReference
Set RepRef = RepInst.ReferenceInstanceOf
Dim oPart As Part
Set oPart = RepRef.GetItem("Part")

' Find Parameters, parameters are collections of Parameter objects.
' We can either get them by their name, or by their index, which is their position in the collection.
Dim aLength, viewDim, viewMargin, scaleMode, parScales, parLengths As Parameter
Set aLength = oPart.Parameters.Item("AngleLength")

' Every vpmreference has a Knowledge Objects collection, this is how we get the Knowledge object
Dim knowObjects As KnowledgeObjects
Set knowObjects = RefonRoot.GetItem("KnowledgeObjects")
Dim parSet As ParmsSet
' this is how we get the parameter set located in a Knowledge object
Set parSet = knowObjects.GetKnowledgeRootSet(True, kweParametersType)
Set viewDim = parSet.Collection.Item(1)
Set viewMargin = parSet.Collection.Item(2)
Set scaleMode = parSet.Collection.Item(3)
Set parScales = parSet.Collection.Item(4)
Set parLengths = parSet.Collection.Item(5)

' Collect the scale and length values into two arrayes.
' This is how we define the dimension of the array and we popolate it
Dim scaleValues(), lengthValues() As Variant
ReDim scaleValues(parScales.GetEnumerateValuesSize() - 1)
parScales.GetEnumerateValues (scaleValues)
ReDim lengthValues(parLengths.GetEnumerateValuesSize() - 1)
parLengths.GetEnumerateValues (lengthValues)

' Retreive the drawing
Dim RepInsDrw As VPMRepInstance
Set RepInsDrw = RefonRoot.RepInstances.Item(2)
Dim RepRefDrw As VPMRepReference
Set RepRefDrw = RepInsDrw.ReferenceInstanceOf
Dim drwRoot As DrawingRoot
Set drwRoot = RepRefDrw.GetItem("CATDrawingAccess")
Dim drwSheet As DrawingSheet
Set drwSheet = drwRoot.ActiveSheet

' Retreive the views, we can get the view by their name or index
Dim drwFront, drwIso As DrawingView
Set drwFront = drwSheet.Views.Item("FRONT VIEW")
Set drwIso = drwSheet.Views.Item("ISOMETRIC VIEW")

' Define front view x dimension
drwFront.x = (viewDim.Value / 2) + viewMargin.Value

' Selection scale mode
If scaleMode.Value = "FitScale" Then

' Set fit scale
Dim ScaleValue As Double
ScaleValue = Abs(viewDim.Value / aLength.Value)
drwIso.Scale2 = ScaleValue
drwFront.Scale2 = ScaleValue

Else

' Set defined scale
Dim Count As Integer
Count = 0
Do
If aLength.Value >= lengthValues(Count) And aLength.Value < lengthValues(Count + 1) Then
drwFront.Scale2 = scaleValues(Count)
drwIso.Scale2 = scaleValues(Count)
Exit Do
End If
Count = Count + 1
Loop Until Count = UBound(scaleValues)

End If

' Define the text position inside the drawing view
Dim textFront, textIso As DrawingText
Set textFront = drwFront.Texts.Item(1)
Set textIso = drwIso.Texts.Item(1)
textFront.x = -20
textFront.y = aLength.Value / 2
textIso.x = -(aLength.Value / 2)
textIso.y = -200

'Update the drawing