Let’s Start Programming! Forums CATIA Macro Programming Activate solid in a part

This topic contains 30 replies, has 3 voices, and was last updated by  ramzar 3 years ago.

Viewing 15 posts - 1 through 15 (of 31 total)
  • Author
    Posts
  • #1223

    ramzar
    Participant

    Hi all, i’m looking for help because i’m working on a macro to activate a solid in a part. I put a picture in attachments to be clear.

    here is my problem: I want to activate “Solide.42” located in “conv” in part “Sob-XXXX-XX” in my main product “product1”. I have multiple part in my assembly, and i want my macro to go in each part and activate “Solide.42”.

    Here is my macro (not working):

    Sub CATMain()

    Dim productDocument1 as Document
    Dim product1 As Product
    Dim products1 As Products
    Dim selection As Selection

    Set productDocument1 = CATIA.ActiveDocument
    Set product1 = productDocument1.Product
    Set products1 = product1.Products
    Set selection = CATIA.ActiveDocument.Selection

    selection.Search “CATPrtSearch.BodyFeature.NameInGraph=conv,all”

    Dim selection1 As Selection
    Set selection1 = CATIA.ActiveDocument.Selection

    selection1.Search “CATPrtSearch.MechanicalFeature.Activity=FALSE,sel”

    If selection1.Count = 0 Then
    Msgbox “No desactivated features.”
    Else

    If selection1.Count > 0 Then
    Selection1.Clear

    partcount = product1.Products.Count

    Dim i As Integer

    ‘Loop through all parts
    For i=1 to partcount

    ‘Apply design mode to each part
    products1.Item(i).ApplyWorkMode DESIGN_MODE

    Dim documents1 As Documents
    Set documents1 = CATIA.Documents

    Dim partDocument1 As Document
    Set partDocument1 = products1.Item(i)

    Dim part1 As Part
    Set part1 = partDocument1

    Dim bodies1 As Bodies
    Set bodies1 = part1.Bodies <——- i have an error here

    Dim body1 As Body
    Set body1 = bodies1.Item(“conv”)

    Dim shapes1 As Shapes
    Set shapes1 = body1.Shapes

    Dim solid1 As Shapes
    Set solid1 = shapes1.Item(“Solide.42”)

    part1.Activate solid1

    part1.Update

    Next ‘i
    End If
    End if
    End Sub

    If someone could help me getting this macro work, that would be really nice

    If there are some others errors anywhere else feel free to say.

    Thank you!

    Attachments:
    #1233

    Matt
    Participant

    You dim part1 as a part, but you set it equal to a document. I believe the line is failing because there is no Bodies collection in a document. Try Set part1 = partDocument1.part

    #1234

    ramzar
    Participant

    Thanks you for your answer. I just try to put “Set part1 = partDocument1.part” and now the error is in this line.

    I have an other question, is the code “ApplyWorkMode DESIGN_MODE” make it go in part design? because the only way to activate a solid is when you are in a part, not in a product. So i was wondering if this was correct.

    • This reply was modified 3 years, 1 month ago by  ramzar.
    #1238

    Matt
    Participant

    I just noticed you set partDocument1 = a product. You are looping part products but setting the documents…why not set the part products then set the document and then the part?

    Dim partProduct1 as Product ‘add this variable

    For I = 1 to product1.Products.Count
    Set partProduct1 = products1.item(I)
    Set partDocument1 = partProduct1.ReferenceProduct.Parent
    Set part1 = partDocument1.Part
    ‘Continue with your code

    Also I would Dim outside of the For loop…no need to keep dimming over and over

    Sorry, did not see your other question…No applying design mode switches the part from a CGR (basically a 3D picture created in cache mode) to exposing the history and publications so you can work on it (or something like that:).

    If you want to activate the part design workbench you can add the part to the selection then change the workbench:
    CATIA.ActiveDocument.Selection.Clear
    CATIA.ActiveDocument.Selection.Add part1
    CATIA.StartWorkbench (“PrtCfg”)’starts part design
    CATIA.ActiveDocument.Selection.Clear
    ‘Activate your solid

    Before you can put the next part in the part design work bench, you need to switch back to assembly design or an empty part will open
    CATIA.ActiveDocument.Selection.Clear
    CATIA.ActiveDocument.Selection.Add partProduct1
    CATIA.StartWorkbench (“Assembly”)’starts assembly design
    CATIA.ActiveDocument.Selection.Clear

    • This reply was modified 3 years, 1 month ago by  Matt.
    • This reply was modified 3 years, 1 month ago by  Matt.
    • This reply was modified 3 years, 1 month ago by  Matt. Reason: Typos etc
    #1247

    Emmett Ross
    Keymaster

    Thanks for your input Matt! This is what I wanted see when I created the forum – people helping others.

    Ramzar, ApplyWorkMode DESIGN_MODE puts CGRs from visualization mode into design mode. Please see this page: http://www.scripting4v5.com/additional-articles/switch-a-product-to-design-mode/

    #1249

    Matt
    Participant

    Np, Emmett. Lots of people have helped me (and still do)…glad I can start giving back a little. I am glad you finally opened the forum to everyone.

    #1250

    ramzar
    Participant

    Hi Matt, thanks for your answer. Unfortunally, i try to add that:

    Dim partProduct1 as Product ‘add this variable

    For I = 1 to product1.Products.Count
    Set partProduct1 = products1.item(I)
    Set partDocument1 = partProduct1.ReferenceProduct.Parent
    Set part1 = partDocument1.Part
    ‘Continue with your code

    But it dosent work.

    Also, i try:
    CATIA.ActiveDocument.Selection.Clear
    CATIA.ActiveDocument.Selection.Add part1
    CATIA.StartWorkbench (“PrtCfg”)’starts part design
    CATIA.ActiveDocument.Selection.Clear
    ‘Activate your solid

    And it dosent work either. I don’t really know what to do from now!

    (My CATIA is in french, i don’t know if it change something writing a macro)

    • This reply was modified 3 years, 1 month ago by  ramzar.
    #1252

    Matt
    Participant

    Can you post the errors and the lines they occur at? Do you message boxes earlier in the code work properly? Also, are you writing in VBA or VBScript/CATScript?

    #1253

    Matt
    Participant

    looking at the code again, I think there is another problem at the start:

    This searches for all bodies named “conv” and adds them to the selection
    Set selection = CATIA.ActiveDocument.Selection
    selection.Search “CATPrtSearch.BodyFeature.NameInGraph=conv,all”

    This second selection variable is really the same as the first selection variable. It looks like this one is searching everything you had selected from the previous search and is looking for features that are deactivated and it will select those deactivated features.
    Dim selection1 As Selection
    Set selection1 = CATIA.ActiveDocument.Selection
    selection1.Search “CATPrtSearch.MechanicalFeature.Activity=FALSE,sel”

    The problem is, when the second search finds that no features are deactivated…it has 0 objects in the selection! So when you get to your if statement…

    If selection1.Count = 0 Then
    Msgbox “No desactivated features.”
    Else

    Does the Msgbox “No desactivated features.” get shown on your screen?

    If it does that means there is nothing in the selection…so your next If statement will surely not work because there are 0 objects in the selection.

    If selection1.Count > 0 Then

    I’ll look at the code.

    #1254

    Matt
    Participant

    Ramzar,
    I am not sure of everything you want to do with this code, but it seemed like there were some lines that were not needed. Please see the code below. I rewrote it and changed the variable names to reduce confusion for me. If you can post if you are using VBA or CATScript/VBScript, that is very important. One other thing to note, it appears this blog changes quotation and apostrophe characters when you copy/paste code…this could also contribute to your issues.

    Sub CATMain()

    Dim oRootProductDocument as Document
    Dim oRootProduct As Product
    Dim oPartProducts As Products
    Dim oPartProduct As Product
    Dim oPartDocument As Document
    Dim oPart As Part
    Dim oSelection As Selection

    Dim oShape as Shape

    Dim i As Integer

    Set oRootProductDocument = CATIA.ActiveDocument
    Set oRootProduct = oRootProductDocument.Product
    Set oPartProducts = oRootProduct.Products
    Set oSelection = CATIA.ActiveDocument.Selection

    ‘If you are using cache apply design mode to the entire assembly instead of each part
    oRootProduct.ApplyWorkMode DESIGN_MODE

    ‘Lets wait to check if features are deactivated

    ‘Loop through all parts
    For i = 1 to oPartProducts.Count
    Set oPartProduct = oPartProducts.item(i)
    Set oPartDocument = oPartProduct.ReferenceProduct.Parent
    Set oPart = oPartDocument.Part
    Set oShape = oPart.FindObjectByName(“Solide.42”)

    If TypeName(oShape) <> “Nothing” Then
    oSelection.Clear
    oSelection.Add oPart
    CATIA.StartWorkbench (“PrtCfg”) ‘starts part design
    oSelection.Clear

    oPart.InWorkObject = oShape
    ‘Do something to oShape
    End if

    oSelection.Clear
    oSelection.Add oPartProduct
    CATIA.StartWorkbench (“Assembly”) ‘starts assembly design
    oSelection.Clear

    oPart.Update ‘Be careful updating the entire part…if there is something that
    ‘won’t update, the macro will fail. You can update a specific object using
    ‘oPart.UpdateObject oShape
    Next

    End Sub

    #1262

    ramzar
    Participant

    Hi, thank you Matt for all your answer and your devotion. It’s really appreciated. To answer all your question:

    for the search, i only got the “No desactivated features” if there is really no desactivated feature. This part work.

    I try to use CATScript. I have no experience with VB or anything close to VB, CATScript or VBScript. I’m trying and i’m learning at the same time. Not as much easy as i was thinking!!!

    The thing that i want to do with this macro is: In my main product i have multiple part. In my part, i have “Solide.42” desactivated. In one shot, i want to ativate all “Solide.42” because the only way to do it for now is to go in each part and then activate it. It’s a long process and i want to cut some time because somethime we have like 100-150 even 200 part with “Solide.42”.

    Thank you for your macro, i’ll thy it soon.

    (Sorry for my bad english, i hope that you understanding me well)

    Again, thank you for you help, it’s really appreciated!

    #1263

    ramzar
    Participant

    I just try your macro and it work! it go in each part but the screen is flashing when it goes in each part, is there a way to stop that. Also, i try to put something after “‘Do something to oShape” : oPart.Activate.oShape but it dosent work.

    Seriously, i have to that you again for your help, you can’t imagine how it gonna be useful!

    • This reply was modified 3 years, 1 month ago by  ramzar.
    #1265

    Matt
    Participant

    I don’t speak any other languages, so I am sure your English is better than if I were to try to speak your native language:)

    It is no problem…I had some time and I like helping and learning.

    Now I understand what you are trying to accomplish:) In the attached code I added turning off refreshing the display, checking if the shape is deactivated, and activating it if it is deactivated. Depending on what version of CATIA you are using, you may be able to turn off the Highlight Selected Object using something like CATIA.HSOSynchronized = False…but you have to turn it back on at the end of the macro using something like CATIA.HSOSynchronized = True. But, again, this depends on the version of CATIA. I personally haven’t had any luck doing this.

    So you are aware, VBA, CATScript/catvbs/VBScript are very similar. VBA is more advanced and has more features and functions. It also has intellisense so as you type your code it pops up available options…like when you text message on a phone and it tries to decide what you are typing. It is very helpful for those who are just starting to write code. For example, as you were typing the line oPart.Activate.oShape, after you typed oPart.Activate. intellisense would either popup all the available options for this method OR nothing would popup telling you that you were doing something incorrectly. So you would try oPart.Activate and put a space…still nothing would popup. Then you try oPart.Acitvate( and suddenly intellisense would popup some options or a string that tells you the type of argument you need. You can also stop your macro where ever you want and use the locals window to debug issues. You can highlight a word and hit F1 key to go to VBA help or if it is a CATIA specific function it will take you into the CATIA Automation chm file where you can browse everything to do with CATIA VB. Things aren’t always clear, but it can be very helpful.

    CATScript/catvbs/VBScript are not as advanced and do not contain the same functionalities as VBA. For example, in VBA you can add a collection of all your shapes so you can loop through them over and over to gather different properties.
    CATScript/catvbs/VBScript doesn’t have a collections class so you have to use arrays which is more difficult (in my opinion). They also do not give you intellisense so it makes it very difficult. Do debug in CATScript/catvbs/VBScript you really can only use message boxes to check your code.

    You get to VBA by pressing alt+F11 in catia. It will look difficult but is very helpful.

    One other thing I like to do is map out exactly what I need my macro to do, then “eat the elephant one bite at a time”. Start small, go step by step writing your code. Once you get a step to work, move on to the next. When you have something that works, go back and organize it/clean it up.

    Anyway, here is the code. Let us know how it works.

    Sub CATMain()

    Dim oRootProductDocument as Document
    Dim oRootProduct As Product
    Dim oPartProducts As Products
    Dim oPartProduct As Product
    Dim oPartDocument As Document
    Dim oPart As Part
    Dim oSelection As Selection
    Dim oBodies as Bodies
    Dim oBody as Body
    Dim oShapes as Shapes
    Dim oShape as Shape

    Dim i As Integer

    Dim bIsInactive As Boolean

    Set oRootProductDocument = CATIA.ActiveDocument
    Set oRootProduct = oRootProductDocument.Product
    Set oPartProducts = oRootProduct.Products
    Set oSelection = CATIA.ActiveDocument.Selection

    ‘Turn off refresh display
    CATIA.RefreshDisplay = False

    ‘If you are using cache apply design mode to the entire assembly instead of each part
    oRootProduct.ApplyWorkMode DESIGN_MODE

    ‘Loop through all parts
    For i = 1 to oPartProducts.Count
    Set oPartProduct = oPartProducts.item(i)
    Set oPartDocument = oPartProduct.ReferenceProduct.Parent
    Set oPart = oPartDocument.Part
    Set oShape = oPart.FindObjectByName(“Solide.42”)

    If TypeName(oShape) <> “Nothing” Then
    oSelection.Clear
    oSelection.Add oPart
    CATIA.StartWorkbench (“PrtCfg”) ‘starts part design
    oSelection.Clear

    bIsInactive = oPart.IsInactive(oShape)
    If bIsInactive = True Then
    oPart.Activate(oShape)
    End if

    End if

    oSelection.Clear
    oSelection.Add oPartProduct
    CATIA.StartWorkbench (“Assembly”) ‘starts assembly design
    oSelection.Clear

    oPart.Update ‘Be careful updating the entire part…if there is something that
    ‘won’t update, the macro will fail. You can update a specific object using
    ‘oPart.UpdateObject oShape
    Next

    ‘Turn on refresh display
    CATIA.RefreshDisplay = True

    End Sub

    #1266

    ramzar
    Participant

    Thank you for all the info. I didn’t knew for Alt+F11! that’s gonna help me alot. All my macro i did was is CATScript but now i’ll do it in VBA

    The macro don’t work. I got no error but “Solide.42” dosen’t activate. There is a good part done, i don’t think we are far away from this macro work.

    And i work with Catia V5 R21

    • This reply was modified 3 years, 1 month ago by  ramzar.
    #1268

    Matt
    Participant

    Paste it into vba and try running it. Vbscript usually skips over lines that fail, instead of giving you an error. VBA will highlight the line that fails. I have never changed the activity of a feature, so the syntax may be wrong. I have also never used IsInactive.

    The issue sounds like it is here:
    bIsInactive = oPart.IsInactive(oShape)
    If bIsInactive = True Then
    oPart.Activate(oShape)

    Keep us posted

    • This reply was modified 3 years, 1 month ago by  Matt. Reason: Typo
Viewing 15 posts - 1 through 15 (of 31 total)

The forum ‘CATIA Macro Programming’ is closed to new topics and replies.