0

I'm trying to make a Publication of points, line and planes in a geometrical set of Part using a VBA script.

I get an error on of the SetDirect method of publications1, where the error says "The method SetDirect failed".

The Publication is added to the list, but the Element is empty.

I've tried publications1.SetDirect("centerPoint1", reference1) and publications1.SetDirect "centerPoint1", reference1.

Here's the documentation; http://catiadoc.free.fr/online/interfaces/interface_Publications.htm

What am I doing wrong? I think it could be the making of reference1? Is it possible to make Publications using a VBA script?

Image of the part where I want to make the Publication

enter image description here

The VBA Code

 Sub publicationTest2()


Set CATIA = GetObject(, "CATIA.Application")
Set documents1 = CATIA.Documents

Set partDocument1 = documents1.Item("Part1.CATPart")

Set part1 = partDocument1.Part

Set hybridBodies1 = part1.HybridBodies
Set hybridBody1 = hybridBodies1.Item("arr_set")
Set hybridShapes1 = hybridBody1.HybridShapes
Set hybridShapePointOnPlane1 = hybridShapes1.Item("centerPoint1")

'Set publications1 = CATIA.ActiveDocument.Product.Products.Item(partName).Publications
Set productDocument1 = CATIA.ActiveDocument
Set product1 = productDocument1.Product
Set products1 = product1.Products
Set product2 = products1.Item("Part1.1")

Set publications1 = product2.Publications


Set reference1 = hybridShapePointOnPlane1
publications1.Remove ("centerPoint1")
Set publication1 = publications1.Add("centerPoint1")
part1.Update
Set publ1 = publications1.SetDirect("centerPoint1", reference1)

End Sub
braX
  • 11,506
  • 5
  • 20
  • 33

1 Answers1

0

Publication is all about determining the magic reference string in the Product context. Most of the time it will be... product.name /! thing.name.

if you have multiple features with the same name, or very deep tree structure, you may have to enhance this using the Part.Parameters.GetNameToUseInRelation method, but for most of my past VBA work, this subroutine has been sufficient:

Sub PublishSomething(thing, partProd As Product, Optional pubName As String = "")

  Dim oProd As Product 
  Set oProd = partProd.ReferenceProduct  'just in case is instance product 
  Dim ref As Reference
  Dim pubString As String
  pubString = oProd.name & "/!" & thing.name
  Set ref = oProd.CreateReferenceFromName(pubString)

  Dim pubs As Publications
  Set pubs = oProd.Publications

  If Len(pubName) = 0 Then pubName = thing.name

  Dim thePub As Publication

  On Error Resume Next
  Set thePub = pubs.item(pubName)

  If Err.Number <> 0 Then
    Err.clear
    Set thePub = pubs.add(pubName)
  Else
    Exit Sub
  End If

  On Error GoTo 0
End Sub

P.S. Recording macros can be very helpful in figuring this stuff out.

C R Johnson
  • 964
  • 1
  • 5
  • 12