0

I have an assembly including 2 parts. I want to take a face in first part as an input for designing second one. Interactively in the CATProduct Workbench, I only choose the face I want in the first part, assumming that I am running a Pad command and need the face as the reference plane, then a copy of the face is automatically created in the workbench I am designing on (for the second part), through which the Pad command is referenced and completed. In VBA programming, if I use Selection for the face in the first part, there will be a error as the face is not in the workbench (a copy is not created automatically as above). If anyone has an idea or workaround for this problem, please let me know! Thanks for your help in advance!

Here is a copy of my code:

Sub CATMain()
    Dim productDocument1 As ProductDocument
    Set productDocument1 = CATIA.ActiveDocument
    
    Dim product1 As Product
    Set product1 = productDocument1.Product
    
    Dim products1 As Products
    Set products1 = product1.Products
    
    Set partDocument1 = products1.Item(1)
    MsgBox partDocument1.PartNumber
    
    Dim partDoc1 As PartDocument
    Set partDoc1 = partDocument1.GetMasterShapeRepresentation(True)
   
    
    'Dim partDocument2 As PartDocument
    Set partDocument2 = products1.Item(2)
    MsgBox partDocument2.PartNumber
    
    Dim partDoc2 As PartDocument
    Set partDoc2 = partDocument2.GetMasterShapeRepresentation(True)
    
    
    Dim oSel_1 As Object
    Set oSel_1 = partDoc1.Selection

        
    Dim InputObjectType(0) As String
    Dim Status As String
    InputObjectType(0) = "Face"  'the needed face is cylindrical
    Status = oSel_1.SelectElement4(InputObjectType, "Select a face", "Select face", True, partDoc2)
    'error keep occuring here with the message "Type mismatch".
    
    MsgBox Status
    If (Status = "Normal") Then
        partDoc2.Selection.Copy
        oSel_1.Clear
        oSel_1.Add partDoc1.Part.HybridBodies.Item(1) 'first hybrid body
        oSel_1.PasteSpecial "CATPrtResult"
    End If
End Sub
Ike
  • 9,580
  • 4
  • 13
  • 29
  • Copy the face in the first part, paste it into the second part (_.PasteSpecial "CATPrtResult"_) and use this linked face as reference for your geometrie. – Shrotter Jul 03 '22 at 08:32
  • Yes, that is also what I am trying to do, but I am stuck in where I should paste the copied faced in the second part. I pasted it into PartBody, GeometricalSet (using Selection object) but nothing happen in the specification tree, which means no linked face was created in the second part, right? – Hung Pham Vu Jul 03 '22 at 10:07

1 Answers1

0

Here a example how to copy an plane from one part in a product to another. Be aware: some error handlers are missing, only works with geometry whit a own entry in the tree (no BREP)

Sub CATMain()

Dim ProdDocument as ProductDocument
Dim TargetPart as Part
Dim TargetHBodie as Hybridbody

Dim oSel as Object
Dim Filter(0) as String
Dim SelStatus as String

Set ProdDocument = CATIA.ActiveDocument
Set oSel = ProdDocument.Selection

Filter(0) = "Part"

SelStatus = oSel.SelectElement2(Filter, "Please select target part", false)

if SelStatus <> "Normal" then
    MsgBox "Selection canceled"
    Exit Sub
end if

Set TargetPart = oSel.Item2(1).Value
Set TargetHBodie = TargetPart.Hybridbodies.Item(1)

Filter(0) = "Plane"

SelStatus = oSel.SelectElement2(Filter, "Please select plane to copy", false)

if SelStatus <> "Normal" then
    MsgBox "Selection canceled"
    Exit Sub
end if

oSel.Copy
oSel.Clear
oSel.Add TargetHBodie
oSel.PasteSpecial "CATPrtResult"

End Sub
Shrotter
  • 350
  • 3
  • 9
  • I tried to follow your concept in your code, but it didn't work. I editted my post above by adding my code, in which the error keep being in the "SelectElement4" with Typemismatch error (I don't know why). I am programming for creating parts in the same assembly as normal in interactive designing. Could you take a moment to check it? – Hung Pham Vu Jul 04 '22 at 02:36
  • @HungPhamVu _SelectElement4_ is for selecting in an other window. The 4th parameter is filled during the selection (may dimeson is as a _document_ only). – Shrotter Jul 04 '22 at 05:58
  • @HungPhamVu I edited my post to select in a product a plane to copy – Shrotter Jul 04 '22 at 06:46
  • I copied entire your code and ran it, the same error still occur at SelectElement2 (Typemismatch), which mean there is something wrong in the syntax, right? (you can see video here: https://drive.google.com/file/d/1ALyaivberD-q6zC6j560NAYtoM2OwJVO/view?usp=sharing). I don't know why because I also used the similar syntax in an another code and it worked. – Hung Pham Vu Jul 04 '22 at 07:56
  • So try to dimension _Filter_ as variant. – Shrotter Jul 04 '22 at 08:01
  • Yeah, this time it works. But what I need is a face, not a plane. When I change the Filter string to "Face", nothing happens as described in the beginning of this post. You said I can't work with BREP elements, right? Could you tell me clearer why? – Hung Pham Vu Jul 04 '22 at 08:15
  • You can only copy elements which are located in the tree. If you want to "copy" a BREP, you have to create an _extract_ of this BREP in the source part and copy this extract. – Shrotter Jul 04 '22 at 08:26
  • Could you give me an example of making an extract of a BREP element? I tried to do that without success. If you can, please check some following images, which describes more about my problem (https://drive.google.com/file/d/1IP5SrUJHUpO1AS7iBggEPeCLqEHQ02Dk/view?usp=sharing and https://drive.google.com/file/d/13tDYbprnz3oFOsHnL-HthIRr8-wMlhOk/view?usp=sharing) – Hung Pham Vu Jul 05 '22 at 16:43
  • @HungPhamVu no I can't. I try to avoid BREPs. – Shrotter Jul 05 '22 at 17:02