Search code examples
vbacatia

CATIA v5. Macro for extracting value of 3Dpart's parameter into a text box on the drawing


I've been trying to do what the title describes for over a day now and I can't seem to figure it out.

Situation:

I have a 3D part with multiple user-made parameters as string. I create a new drawing with front, top & isometric view. I wish to create a macro that reads the string values of the parameters of my 3D part and writes them in specific locations on the drawing.

Work so far:

I'm able to have a macro summon text to my drawing, but I can't figure out how to, while in the VB environment, extract a string value from a user-made parameter in my 3D part. I've tried to use

myValue = material.Value

Where "material" is the parameter of my 3D part but I'm not able to get a return. I do not know what to declare and how to reference to the parameter properly.

Furthermore, I'm capable of writing plain text on my drawing with a macro by writing this:

Set myText = MyDrawingViews.ActiveView.Texts.Add("description", 22, 38)

I get a text saying "description" on my drawing in the intended location, but I can't figure out how to drive the text with a variable instead. When I try:

dim myValue as string
myValue = "description"
Set myText = MyDrawingViews.ActiveView.Texts.Add(myValue, 22, 38)

I do not get a return.

I've been trying but I can't seem to get anywhere, any help would be greatly appreciated.


Solution

  • You need to get a reference to the Parameter from the Part or Product that you want in the text. Also, you should use the InsertVariable method of a DrawingText object to link a parameter. When the parameter changes in the part, it can be updated in the drawing.

    Here's a simple Sub that can accomplish what you want(you can modify it to accomplish what you want more specifically):

    Sub AddTextWithLinkedParameter(dViewToContainTheText As DrawingView, xPos, yPos, Optional param As Parameter)
    
        Dim dtext As DrawingText
        Set dtext = dViewToContainTheText.Texts.Add("", xPos, yPos)
    
        If Not param Is Nothing Then
            dtext.InsertVariable 0, 0, param
        End If
    
    End Sub
    

    Here's some sample code to test it:

    Sub testParameterText()
        Debug.Assert False
        '
        'Manually Activate the Part Document
        'that contains a string parameter called "Property
        '
        Dim myParam As Parameter
        Dim partDoc As PartDocument
        Set partDoc = CATIA.ActiveDocument
        Set myParam = partDoc.Part.Parameters.Item("Property")
    
        Debug.Assert False
        'manually switch to a drawing document
    
        Dim dDoc As DrawingDocument
        Set dDoc = CATIA.ActiveDocument
    
        Dim dSheet As DrawingSheet
        Set dSheet = dDoc.Sheets.ActiveSheet
    
        Dim dView As DrawingView
        Set dView = dSheet.Views.Item("Main View")
    
        AddTextWithLinkedParameter dView, 20, 20, myParam
    
    End Sub