FTA Catia R24 Associative Front view (VBA) - 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.

Related

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

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

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.

VBA Word: Change Data of charts

I want to change the data of a chart in a Word Document, but I can't find the right way to address my charts. I tried several techniques, but nothing worked. (I´d love to open a ExcelSheet in which I can just change the Data)
So to put it all together: I want to change the data (not the source), of a MS Word chart, which looks like that:
Edit(13.8.):
After request, I try to give you some "reference Code" to work with.
Sub ChangeChart()
Dim aktDocument As Document
Dim chrt As Chart
Dim SourceSheet As Excel.Worksheet
Set aktDocument = ActiveDocument
Set SourceSheet = aktDocument.Shapes(1).Chart.OpenSourceData 'I know it´s not that easy
SourceSheet.Range("B5") = newStuff
aktDocument.Shapes(1).Chart.SetSourceData = SourceSheet
End Sub
I know this may sounds utopic and ridiculous, but I just don´t know, how to address the chart in the right way, or to even work with it properly.
Edit(15.08):
Even after recreating the old charts, the following code is not able to find a shape which has a chart. And therefore it stops when the index is out of range.
Sub Test()
i = 0
Do While i < 100
i = i + 1
If ActiveDocument.Shapes(i).HasChart Then
MsgBox "found one!"
End If
Loop
End Sub
Solution(30.08.):
The answer from #Cindy Meister was the solution to my problem. After further working with it, I came to the problem, that the ChartData always opens on the screen, while running the code.
Just for reference this question was my workaround.
All Office applications use the Excel engine to create and manage charts. In Word, charts can be formatted in-line with the text or with text wrap formatting. In the former case, a chart object needs to be addressed via the InlineShapes collection, in the latter via the Shapes collection.
Since your sample code uses Shapes(1) I've used that in the code snippet below. If it's not certain that the first Shape in the document is the chart, but you've assigned the Shape a name, you can use that as the index value (for example Shapes("MyChart"). Or you can loop the Shapes collection and check HasChart.
HasChart returns True if the Shape (or InlineShape) is a Chart. It's then possible to set Shape.Chart to an object variable. The chart's data can be accessed using Chart.ChartData.Activate - if you don't use Activate it's not possible to access the data when the chart's worksheet is stored in the Word document. Only then can Chart.ChartData.Workbook return a workbook object, and through that the worksheet can be accessed using ActiveSheet. From that point on, it's like working with the Excel object model.
Sub ChangeChart()
Dim aktDocument As Document
Dim shp As Word.Shape
Dim chrt As Word.Chart
Dim wb As Excel.Workbook, SourceSheet As Excel.Worksheet
Set aktDocument = ActiveDocument
Set shp = aktDocument.Shapes(1)
If shp.HasChart Then
Set chrt = shp.Chart
chrt.ChartData.Activate
Set wb = chrt.ChartData.Workbook
Set SourceSheet = wb.ActiveSheet
SourceSheet.Range("B5").Value2 = newData
End If
End Sub

Catia V5 macro to access axis-system inside a geometric set

Using a Catia V5 macro I want to save axis system and other geometric elements inside a geometic set to a txt file. I'm able to access the geometric elements like points and lines but not the axis system(Shown in image) .
'Procedure to access the geometic elements inside a geometric set
Dim prtDoc As Part
Dim hbds As HybridBodies
Dim hbs As HybridShapes
Set hbs = hbds.GetItem(objSel.Item2(1).Value.Name).HybridShapes
To access a geometric element and get its type
MsgBox TypeName(hbs.Item(i))
But how to access the axis system?
Axis Systems can't be found inside a Geometrical Set by looping through child items via the CATIA API.
A hybridBody (Geometrical Set) only contains HybridShapes, HybridBodies and Sketches.
You can see Lines, Points, and such other items because they are all HybridShapes.
As far as I know, the only way of getting the Axis System object inside a Geometrical Set is through the Selection.Search method.
Assuming you have the CSYS Geometrical Set in your selection, you can do the following:
Dim Selection as INFITF.Selection
Dim AxisSystem as INFITF.AxisSystem
'Initialize Selection and other code here
Selection.Search("'Part Design'.'Axis System';sel")
for i = 1 to Selection.Count2
set AxisSystem = Selection.Item2(i).Value
'...
Next

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