Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

basic vba question

Status
Not open for further replies.

MerolaAlba

Automotive
Oct 19, 2017
12
PT
Hi everyone,


I have a basic vba question.

I recorded a macro in CATVBA that allows the user to select a specific CATPart in an assembly, I'm going to insert it in a GUI via VBA editor to make a Slider "graphical menu" to speed up process of going up and down the spec tree and to make it attractive to management ;-)

I'm trying to alter a user selected parameter of a selected CATPart in an assembly via CADSelection. I've recorded the macro several times and tried to incorporate oSelection in several places and tweaked around with this for a while now, but i can't seem to finish this.

Am i declaring oSelection in the incorrect place, or am i missing something else?



My tree is:

Product;

-Part;

-Part_Teste_2;

-Part_Teste_3;

-Part_Teste_4;

-Part_Teste_5;

All of the above "Part_Teste" have 3 parameters one of them is called "Comprimento"

How do I change make sure that the macro alters the selected CATPArt's parameter instead of ("Part_Teste_3.CATPart")?

The macro that I have so far is:

Code:
Language="VBSCRIPT"

Sub CATMain()

Set oProductDoc = CATIA.ActiveDocument

Set oProd = oProductDoc.Product

Set oDocs = CATIA.Documents

Set oSelection = CATIA.ActiveDocument.Selection

If oSelection.Count < 1 then

    MsgBox "Pick some components using cad selection.","No components were selected"

Else

    Set oPartDoc = oDocs.Item("Part_Teste_3.CATPart")

    Set oPart = oPartDoc.Part

    Set oParam = oPart.Parameters

    Set oLength = oParam.Item("Comprimento")

    oLength.Value = 50.000000

End If

oSelection.Clear

oProd.Update

End Sub
 
Replies continue below

Recommended for you

Hello,
just try something like this:

Code:
If oSelection.Count < 1 then
    MsgBox "Pick some components using cad selection.","No components were selected"
Else
    Set oPartDoc = oSelection.Item(1).Value
    Set oPart = oPartDoc.Part
    Set oParam = oPart.Parameters
    Set oLength = oParam.Item("Comprimento")
    oLength.Value = 50.000000
End If

If you would like to change parameter for more selected elements, then you have to call your code in loop.

Tesak
- Text along a curve for Catia V5
 
Hi @tesak,

Thanks for your reply.
I get the following error on this line:


The object doesn't support this property or method.

Code:
Set oPart = oPartDoc.Part

Should I be declaring oPartDoc somewhere above?


Regards,
 
Sorry I did mistake and I am mixing apples with pears (because I have no computer to test it). Try like this:

Code:
If oSelection.Count < 1 then
    MsgBox "Pick some components using cad selection.","No components were selected"
Else
    Set oPart = oSelection.Item(1).Value.ReferenceProduct.Parent.Part
    Set oParam = oPart.Parameters
    Set oLength = oParam.Item("Comprimento")
    oLength.Value = 50.000000
End If

You do not need document oPartDoc at all, because from selection you should get your part directly.





Tesak
- Text along a curve for Catia V5
 
@tesak
Using your suggestion and a For Loop i was able to get the code work.
Here's the updated code in .catvbs

Code:
Language="VBSCRIPT"

Sub CATMain()

Set oProductDoc = CATIA.ActiveDocument
Set oProd = oProductDoc.Product
Set oDocs = CATIA.Documents
Set oSelection = CATIA.ActiveDocument.Selection

If oSelection.Count < 1 then
		
	MsgBox "Pick some components using cad selection.","No components were selected"

Else
	Dim I
	For I = 1 To oSelection.Count
		Set oPartDoc = oSelection.Item(I).Value.ReferenceProduct.Parent
		Set oPart = oPartDoc.Part
		Set oParam = oPart.Parameters
		Set oLength = oParam.Item("Comprimento")
		oLength.Value = 50.000000
	Next
End If

oSelection.Clear

oProd.Update

End Sub

With it i can select any of the CATParts (using cadselection in the spectree) and provided that they have the parameter

"Comprimento" in them it will update the CATPart accordingly.

In the 1st attachment is a prtscr of the spec tree:
Link
1 - CATProduct;
1 - CATPart;
4 - CATParts:
Part_Teste_2;
Part_Teste_3;
Part_Teste_4;
Part_Teste_5;
Each of these has 3 Parameters:
Comprimento (Length)
Largura (Width)
Altura (Height)

In the 2nd attachment is a prtscr of what the menu is and how it should work.
Link
Now "all that's left", is to transfer that specific code to the catvba project, make appropriate changes and link it to the respective textboxes in the VBA Project.

Anything i should avoid doing, or do you have any advice on how to proceed further?
 
I think you are on a good way. You can perhaps make it more robust by adding some error handling (if it is going to be used by more users), but it works and this is important. With VBA and user interface you make it pretty and user friendly which is always good, especially if you need to present it to somebody :).

Tesak
- Text along a curve for Catia V5
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top