Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code from VBA to VBScript 1

Status
Not open for further replies.

romaleee

Mechanical
May 13, 2016
37
Hello,

here I have some code that I work on and I've hit a wall.
What it does. The idea is that the "break view" in drawing always has same size no mater if the referenced part changes its length. So I decided to automatically remove a existing break and create a new one according to new "3d part length". I want to put the code in VBScript (Macro with Arguments) and call it with reaction. I've written the code and it works in VBA. However when i copy the code in VBScript I get several errors that I do not know how to solve. Please help me someone :).

Code:

Code:
Sub CATMain()

' first part of code used to define a View. It will not be needed later when I plan to inport rest of the code in VBScript - Macro With arguments
Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = CATIA.ActiveDocument
Dim drawingSheets1 As DrawingSheets
Set drawingSheets1 = drawingDocument1.Sheets
Dim drawingSheet1 As DrawingSheet
Set drawingSheet1 = drawingSheets1.Item("Sheet.1")
Dim drawingViews1 As DrawingViews
Set drawingViews1 = drawingSheet1.Views
Dim drawingView1 As DrawingView
Set drawingView1 = drawingViews1.Item("Bottom view")
Dim parameters1 As Parameters
Set parameters1 = drawingDocument1.Parameters
Dim length1 As Length
Set length1 = parameters1.Item("Drawing\Duljina_letvice")
Dim drawingViewGenerativeBehavior1 As DrawingViewGenerativeBehavior
Set drawingViewGenerativeBehavior1 = drawingView1.GenerativeBehavior
Set drawingViewGenerativeBehavior1Variant = drawingViewGenerativeBehavior1

'define array for positions of horizontal dim lines
Dim visine() As String
ReDim visine(1, 0)
'call sub for extracting the positions
Call GetPositions(drawingView1, visine())

'unbreak view
drawingViewGenerativeBehavior1Variant.UnBreak
Dim koord As Double
'set position of upper break line
koord = length1.Value - 60

'define new broken view
Dim arrayOfVariantOfDouble1(7)
arrayOfVariantOfDouble1(0) = 0#
arrayOfVariantOfDouble1(1) = 50#
arrayOfVariantOfDouble1(2) = 9#
arrayOfVariantOfDouble1(3) = 50#
arrayOfVariantOfDouble1(4) = 0#
arrayOfVariantOfDouble1(5) = koord
arrayOfVariantOfDouble1(6) = 0#
arrayOfVariantOfDouble1(7) = koord
drawingViewGenerativeBehavior1Variant.DefineBrokenView arrayOfVariantOfDouble1, 0#, 1#

'define new position of view
drawingView1.y = length1.Value / 2 + 125#

'call sub for updating the dim value position
Call UpdatePositions(drawingView1, visine())

End Sub

Sub GetPositions(oView As DrawingView, oArray() As String)
    Dim drawingDimension1 As DrawingDimension
    Dim oLine As Object
    
    For Each drawingDimension1 In oView.Dimensions
        Set oLine = drawingDimension1.GetDimLine
        'define array that collects coordinates from dimline
        Dim info() As Variant
        ReDim info(5)
        'collect coordinates
        Call oLine.GetGeomInfo(info())
            'Extract coordinate for vertical position
            If Round(info(1), 5) = Round(info(3), 5) Then
                oArray(0, UBound(oArray, 2)) = drawingDimension1.Name
                oArray(1, UBound(oArray, 2)) = info(1)
                ReDim Preserve oArray(1, UBound(oArray, 2) + 1)
            End If
    Next
    ReDim Preserve oArray(1, UBound(oArray, 2) - 1)
End Sub
'sets horizontal dimvalues to proper position
Sub UpdatePositions(oView As DrawingView, oArray() As String)
    Dim drawingDimension1 As DrawingDimension
    Dim oValue As DrawingDimValue
    Dim i As Integer
    For i = 0 To UBound(oArray(), 2)
        Set drawingDimension1 = oView.Dimensions.Item(oArray(0, i))
        'set vertical position of value
        drawingDimension1.MoveValue 0, CDbl(oArray(1, i)), 0, 1
        'restore horizontal position
        drawingDimension1.RestoreValuePosition
    Next
End Sub

How it looks like in VBScript:

VBScript_ldlsck.jpg


Error I get:

VBScript_err_xws4v0.jpg


If I made some irrational mistakes in my coding I apologize in front. I'm not a professional programer :).
Thank you in advance.
Roman
 
Replies continue below

Recommended for you

VBScript has only one data type called a Variant. I am not sure if this is a cause of your problem, but at first I would delete all "As" clauses from variable declarations e.g. " As DrawingDocument", " As DrawingSheet" and so on. If your script is stored as CATScript then As declarations are just ignored (but anyway, all types are Variants).

And also here
Call oLine.GetGeomInfo(info())​
change line to (delete a parenthesis around info variable):
Call oLine.GetGeomInfo(info)​



Tesak
- Text along a curve for Catia V5
 
Thanks for the quick reply. I've learned something new from it but unfortunately the error remains with a little difference.

VBScript_err2_lv9dtz.jpg


Code:
oView as DrawingView, oLength as Length

Code:
Dim drawingViewGenerativeBehavior1
Set drawingViewGenerativeBehavior1 = oView.GenerativeBehavior

Dim visine()
ReDim visine(1, 0)
Call GetPositions(oView, visine)

Set drawingViewGenerativeBehavior1Variant = drawingViewGenerativeBehavior1
drawingViewGenerativeBehavior1Variant.UnBreak

Dim koord, x, y
koord = oLength.Value - 60
y = oLength.Value/2 + 125.000000

Dim arrayOfVariantOfDouble1(7)
arrayOfVariantOfDouble1(0) = 0.000000
arrayOfVariantOfDouble1(1) = 50.000000
arrayOfVariantOfDouble1(2) = 9.000000
arrayOfVariantOfDouble1(3) = 50.000000
arrayOfVariantOfDouble1(4) = 0.000000
arrayOfVariantOfDouble1(5) = koord
arrayOfVariantOfDouble1(6) = 0.000000
arrayOfVariantOfDouble1(7) = koord
drawingViewGenerativeBehavior1Variant.DefineBrokenView arrayOfVariantOfDouble1, 0.000000, 1.000000
oView.y = y
Call UpdatePositions(oView, visine)
End Sub

Sub GetPositions(oView, oArray())
    Dim drawingDimension1
    Dim oLine

    For Each drawingDimension1 In oView.Dimensions
        Set oLine = drawingDimension1.GetDimLine
        Dim info(3)
        [b]Call oLine.GetGeomInfo(info)[/b]
            If Round(info(1), 5) = Round(info(3), 5) Then
                oArray(0, UBound(oArray, 2)) = drawingDimension1.Name
                oArray(1, UBound(oArray, 2)) = info(1)
                ReDim Preserve oArray(1, UBound(oArray, 2) + 1)
            End If
    Next
    ReDim Preserve oArray(1, UBound(oArray, 2) - 1)
End Sub

Sub UpdatePositions(oView, oArray())
    Dim drawingDimension1
    Dim oValue
    Dim i
    For i = 0 To UBound(oArray(), 2)
        Set drawingDimension1 = oView.Dimensions.Item(oArray(0, i))
        drawingDimension1.MoveValue 0, CDbl(oArray(1, i)), 0, 1
        drawingDimension1.RestoreValuePosition
    Next
End Sub
 
Instead of
Code:
Dim info(3)
use
Code:
Redim info(3)
and your code will work.

A problem is that with Dim info(3) you create a static array, but it seems that a procedure expects a dynamic array, which you can create by Redim statement.

Tesak
- Text along a curve for Catia V5
 
Thank you very much Tesak. You saved the day :). 5* for you.
 
@tesak

Hi

since you already helped me with the VBA->VBScipt conversion once, i'd like to ask you one more question. I'm getting an error message for this code in VBS:

Code:
Set osel = CATIA.ActiveDocument.Selection

dim InputObjectType(0)
Dim status
InputObjectType(0) = "AxisSystem"
osel.Clear
status = osel.SelectElement2(InputObjectType, "Select Axis System", False)

End Sub

The error is ...

error_zu9fj3.jpg


This part of code is needed in folowing situation:
I have a reaction "Poziv" that calls VBScript "NewProduct" every time when a product is inserted in assembly.

rel_dkgxuh.jpg


With the upper code user should select an axis system to create a reference for a constraint wich is created afterwards.

Help would be very appreciated.
Thanks in advance
Roman
 
I think you will need to save your script as Macro.CATScript and launch it from the reaction...
In Dictionariy/Messages and Macros there is
LaunchMacroFromDoc()--see if this works...
LaunchMacroFromDoc(Macro.CATScript)


regards,
LWolf
 
This is kind of unfortunate. Because I wanted to have code inside of .CATProduct file. :(
 
Caught up with my ignorance :).

Thanks LWolf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor