Is it possible to insert an attribute link to a part property in a drawing through a VBScript/CATScript macro? [duplicate] - automation

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.

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

Related

How do I deactivate and reactivate several geometrical sets and objects automatically?

I wrote a macro that hides everything in several geometrical sets and the objects and geometrical sets in these first sets except one specific branch. I use this for saving a defined object of a huge and complicated specification tree as a STP file. (See attached below.)
(Small complication in this “Hide_and_Save” macro: adding bodies to my hide-selection works well but for my show-selection it didn’t work the same way. Why would this happen?)
I also wrote a macro that does iterative adjustments. For the iterations I use a Do While Loop and some parameters and measurements. To update these values, I have to update the part/object in every cycle. But there are some construction elements that issue errors until the loop is successfully completed. Therefore I deactivate all the geometrical sets that I don’t need for the iterations (inclusively all children) and later I reactivate them manually.
My goal is to improve automation, so I tried to use my “Hide_and_Save” macro for deactivation and reactivation. This didn’t work. When I record the process, each object is listed in a separate line and deactivated. Since there are more than 350 elements, I would like to avoid this.
How do I deactivate all subelements in a geometrical set (preferably with children) without addressing each element individually?
Attribute VB_Name = "Hide_and_Save"
'_______________________________________________________________________________________
'Title: Hide_and_Save
'Language: catvba
'_______________________________________________________________________________________
Sub CATMain()
'---------------------------------------------------------------------------------------
'Select active Part/Document
Dim myDocument As Document
Set myDocument = CATIA.ActiveDocument
Dim myPart As part
Set myPart = CATIA.ActiveDocument.part
'--------------------------------------------------------------
' Enter file path
Dim filepath As String
filepath = InputBox("Please select memory location", "Input filepath", "...")
If filepath = "" Then 'cancle, abort or empty input
MsgBox "No valid input / cancle !"
Exit Sub
End If
'--------------------------------------------------------------
' Hide/show Objects of Part/Products and save as STEP
' Update Model
CATIA.ActiveDocument.part.Update
' Deklaration of Selections and Properties
Dim selectionShow, selectionHide As Selection
Set selectionShow = myDocument.Selection
Set selectionHide = myDocument.Selection
Dim visPropertySetShow, visPropertySetHide As VisPropertySet
Set visPropertySetShow = selectionShow.VisProperties
Set visPropertySetHide = selectionHide.VisProperties
' Definition of the collection of geometric sets - HybridBodies
Dim hybridBodiesInPart, hybridBodiesInProcess As HybridBodies
Dim hybridBodiesInRS, hybridBodiesInHuelle As HybridBodies
' Definition of individual geometric sets - HybridBody
Dim hybridBodyInPart, hybridBodyProcess, hybridBodyInProcess As HybridBody
Dim hybridBodyRS, hybridBodyInRS As HybridBody
Dim hybridBodyHuelle, hybridBodyInHuelle As HybridBody
' Definition of the collection of 3D-objects - HybridShapes
Dim hybridShapesInHuelle As HybridShapes
' Definition of individual 3D-objects - HybridShape
Dim hybridShapeInHuelle, hybridShapeForm As HybridShape
' Hide objects
Set hybridBodiesInPart = myPart.HybridBodies
For Each hybridBodyInPart In hybridBodiesInPart
selectionHide.Add hybridBodyInPart
Next
Set hybridBodyProcess = hybridBodiesInPart.Item("Process")
Set hybridBodiesInProcess = hybridBodyProcess.HybridBodies
For Each hybridBodyInProcess In hybridBodiesInProcess
selectionHide.Add hybridBodyInProcess
Next
Set hybridBodyHuelle = hybridBodiesInProcess.Item("Huelle")
Set hybridBodiesInHuelle = hybridBodyHuelle.HybridBodies
For Each hybridBodyInHuelle In hybridBodiesInHuelle
selectionHide.Add hybridBodyInHuelle
Next
Set hybridShapesInHuelle = hybridBodyHuelle.HybridShapes
For Each hybridShapeInHuelle In hybridShapesInHuelle
selectionHide.Add hybridShapeInHuelle
Next
Set hybridShapeForm = hybridShapesInHuelle.Item("Form")
visPropertySetHide.SetShow 1 'hide
selectionHide.Clear
' Show objects
selectionShow.Add hybridBodyProcess
selectionShow.Add hybridBodyHuelle
selectionShow.Add hybridShapeForm
visPropertySetShow.SetShow 0 'show
selectionShow.Clear
' Data export as STP
stepAnswer = MsgBox("Should the displayed elements be saved as STEP?", 3 + 0, "Export: Form")
If stepAnswer = 6 Then
myDocument.ExportData filepath & "Form" & ".stp", "stp"
ElseIf stepAnswer = 3 Or stepAnswer = 2 Then 'cancle or abort
MsgBox "cancle !"
Exit Sub
End If
'---------------------------------------------------------------------------------------
MsgBox "Finished !" & vbCrLf & s
End Sub
(Usually I work with Generative Shape Design and use VBA for Macros.)
Each feature has an "Activity" parameter aggregated to it.
Dim oShape as HybridShape
For Each oShape In oGS.HybridShapes
Dim oActivity as Parameter
Set oActivity = oPart.Parameters.SubList(oShape,False).Item("Activity")
Call oActivity.ValuateFromString("False")
Next
Let me add that screwing with Activity of features is not a best practice. I NEVER do this myself. If you have access KBE (Specifically Knowledge Advisor Workbench) you can probably do what you want with Rules/Actions/Reactions, less coding and have a more robust model in the end.

FTA Catia R24 Associative Front view (VBA)

does anyone know how to create Associative Front View in FTA using VBA. I have short macro to create Front View based on the geometry from 3D but this view is isolated and I must manually change it to Associative.
What I would like to have is Associative View created by macro. It is first step to perform macro for power copy.
Bellow my code.
Sub CATMain()
Dim partDocument1 As PartDocument
Set partDocument1 = CATIA.ActiveDocument
Dim part1 As PART
Set part1 = partDocument1.PART
'--------------------------------------------------
'Create Annotation Set
Dim annotationSet1 As AnnotationSet
Set annotationSet1 = part1.AnnotationSets.Add("ISO")
'Debug.Print annotationSet1.Name
Dim theViewFactory As TPSViewFactory
Set theViewFactory = annotationSet1.TPSViewFactory
'--------------------------------------------------
'Create reference plane from selection
Dim ViewPlane As Reference
Set Selection = partDocument1.Selection
Selection.Clear
Selection.Search ("name='Plane.6',all") 'Search plane by name
Set ViewPlane = Selection.Item(1).Value 'Set plane from selection
Selection.Clear
'--------------------------------------------------
'Create Front View
Dim theView As TPSView
Set theView = theViewFactory.CreateView(ViewPlane, 0)
theView.Name = "FrontView"
'--------------------------------------------------
'Create dimension
part1.Update
End Sub
As far I my knowlodge on Catia API go, it is not possible to associate a view using the API.
Also, using Win32 to perform clicks on buttons/comboboxes from handles will not work because to associate the view to a plane/surface you will need to perform a click on the desired location using the Catia User Interface.
the TPSView object on the API is probably one of the worse ones ever exposed, it doesn't have any property. Also, the TPSViewFactory and TPSViews don't have any usefull method to manager views.
So, the short answer is no, you can't do that.

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.
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

Word VBA: Insert a picture without known aspect ratio of file

.addPicture requires width and height, but this could end up distorting the image. Is there any way to use .LockAspectRatio = msoCTrue when adding the file image?
Also, how do you stipulate a page to put the canvas or the picture? Macro recorder needs to be stopped before one can work on images somehow.
Set sCanvas = ActiveDocument.Shapes _
.AddCanvas(Left:=MillimetersToPoints(20), Top:=MillimetersToPoints(20), _
Width:=300, Height:=200)
Set CanvasShapes = sCanvas.CanvasItems
With CanvasShapes
.AddPicture FileName:="C:\somepath\image.png", _
Left:=0, Top:=0, Width:=150, Height:=100
...
End With
Set the canvas size to 0 and lock it's aspect ratio, then add the picture. The canvas will scale to accommodate it. After the image is loaded, then scale the canvas as needed:
Set sCanvas = ActiveDocument.Shapes.AddCanvas(MillimetersToPoints(20), MillimetersToPoints(20), 0, 0)
sCanvas.LockAspectRatio = True
Set CanvasShapes = sCanvas.CanvasItems
With CanvasShapes
.AddPicture "C:\somepath\image.png"
End With
'Scale the canvas here.
You could try to obtain the dimensions using the code here (pasted below for reference)
Sub test()
Dim objShell As Object
Dim objFolder As Object
Dim objFile As Object
Dim fPath As Variant 'MUST be a variant, not a string
Dim fName As String
fPath = "C:\somepath"
fName = "image.png"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(fPath)
Set objFile = objFolder.ParseName(fName)
MsgBox objFile.ExtendedProperty("Dimensions")
End Sub
This will give you the dimensions of the picture which you can then use.
It looks like the output is ? w x h ? so you'll need to parse it, but this should work.
Note that if you want to use a variable to define the file path, it must be declared as a variant, per here
If you check the definition of the AddPicture method you will find that except for FileName all of the arguments are optional, so Height and Width are not required.
Unless you have a specific reason for adding a canvas it is also unnecessary to add a canvas before adding a picture.
Word has no concept of pages so you cannot specify that the picture should appear on a certain page.
Managed to find a way to put a picture to a specific page (page 2 in example below) in MS Word:
Dim pNum as long
pNum = 2
Selection.GoTo What:=wdGoToPage, Count:= pNum

Change cut view text in CATIA

I'm currently working with CATIA V5, and I want to use Macros (VBA), but I have some problems!
My question is: how to change the text of a cut view? (see the picture)
I tried to use : myView.Texts.item(1) to access to this "text" but I think that CATIA dont consider it as text...
I want to change this text without the intervention of the user ( without selections), can I do that?
IME, VBA scripting in drafting workbench is quite tricky at first..."MyTexts" is a collection of DrawingText objects.
MyDrawingText.Text = "MyNewTextValue"
The main trouble you will have is getting a handle on the specific text object that you want to modify. I found that the best way around this is to either scan the entire DrawingTexts collection in the DrawingView, and apply a unique name, DrawingText.Name="UniqueObjectName", or you create the drawing text from the script and you can more easily get a handle on the DrawingText object to put whatever value you want in there. Creating Unique Names makes your drawing more robust for future scripting
MyView.Texts.Count will also be useful to get the item number if the last created DrawingText object(s).
I'm happy to further explain if you need. Good luck!
Update/Edit:
As mentioned above, scripting with the drafting workbench is not always straight forward. It turns out that the callout texts do not exactly live in the DrawingTexts collection of a DrawingView, but they do live somewhere inside the drawing view...In this case, you're trying to edit the "ID" of the section view..That property isn't exposed through VBA either.
There is a hack/work-around which is to search the parent view for drawing texts and and then with some logic, which you'll need to come up with, scan the Selection for the texts you want to change. You should rename then while you're at it, this way it's easier to come back and find them again.
Here's an example starting with an Object Resolution of the Front View (the parent view of the section view)
Sub ChangeCallout()
'---- Begin resolution script for object : Front View
Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = CATIA.ActiveDocument
Dim drawingSheets1 As DrawingSheets
Set drawingSheets1 = drawingDocument1.Sheets
Dim drawingSheet1 As DrawingSheet
Set drawingSheet1 = drawingSheets1.Item("Sheet.1")
Dim drawingViews1 As DrawingViews
Set drawingViews1 = drawingSheet1.Views
Dim drawingView1 As DrawingView
Set drawingView1 = drawingViews1.Item("Front view") 'this is the parent view of the section view
'---- End resolution script
Dim sel As Selection
Set sel = drawingDocument1.Selection
Dim CalloutText As drawingText
sel.Clear 'clear the selection / good practice
sel.Add drawingView1 'add the parent view to the selection
sel.Search "Drafting.Text,sel" 'this will search the current selection for all drawing texts and add them to the selection
Dim thing As Variant
Dim i As Integer
For i = 1 To sel.Count
Set thing = sel.Item2(i)
Set CalloutText = thing.Value
'do some things/logic here to determine if this is a callout with some Ifs or Case statements
'CalloutText.Name = "Useful Unique Name"
'CalloutText.Text = "New Callout Label" 'whatever you want to rename it to
Next
End Sub
the text of the cut view is defined by the view name, to change it you should change the view name as describe bellow:
Sub CATMain()
Dim oDraw As DrawingDocument
Set oDraw = CATIA.ActiveDocument
Dim oSectionView As DrawingView
Set oSectionView = oDraw.Sheets.ActiveSheet.Views.ActiveView
oSectionView.SetViewName "Prefix ", "B", " Suffix"
End Sub
For scanning through the callout texts you can use below lines.
This would select the texts belonging to only callout and doesn't scan through all texts.
Sub CATMain()
Dim drawingDocument1 As Document
Set drawingDocument1 = CATIA.ActiveDocument
Dim selection1 As Selection
Set selection1 = drawingDocument1.Selection
selection1.Search "CATDrwSearch.DrwCallout,all"
selection1.Search "Drafting.Text,sel"
Dim i As Integer
For i = 1 To selection1.Count
MsgBox selection1.Item(i).Value.text
Next
End Sub