×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Code from VBA to VBScript

Code from VBA to VBScript

Code from VBA to VBScript

(OP)
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 --> VBA

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:



Error I get:



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

RE: Code from VBA to VBScript

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
http://scripts4all.eu/txtoncurve/ - Text along a curve for Catia V5

RE: Code from VBA to VBScript

(OP)
Thanks for the quick reply. I've learned something new from it but unfortunately the error remains with a little difference.



CODE --> Atributes

oView as DrawingView, oLength as Length 

CODE --> VBScript

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)
        Call oLine.GetGeomInfo(info)
            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 

RE: Code from VBA to VBScript

Instead of

CODE --> CATScript

Dim info(3) 
use

CODE --> CATScript

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
http://scripts4all.eu/txtoncurve/ - Text along a curve for Catia V5

RE: Code from VBA to VBScript

(OP)
Thank you very much Tesak. You saved the day :). 5* for you.

RE: Code from VBA to VBScript

(OP)
@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 ...



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.



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





RE: Code from VBA to VBScript

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

RE: Code from VBA to VBScript

(OP)
This is kind of unfortunate. Because I wanted to have code inside of .CATProduct file. :(

RE: Code from VBA to VBScript

(OP)
Caught up with my ignorance :).

Thanks LWolf.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources

eBook – How to Choose the Correct Corrosion Testing Method
When designing a metal component, engineers have to consider how susceptible certain alloys are to corrosion in the final product’s operating environment. In a recent study by NACE (National Association of Corrosion Engineers), it was estimated that the direct and indirect costs of corrosion in the United States is approximately 6.2% of the GDP. In 2016, that cost exceeded $1 trillion dollars for the first time. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close