Change cut view text in CATIA - vba

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

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

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

moving paragraph up/down (without copy / paste)

In word I am looking for a keyboard short cut which allows me to move the paragraph in which my cursor currently is one paragraph/line up or down.
I am new to VBA etc, but found this
Sub OutlineMoveUp()
Selection.Range.Relocate wdRelocateUp
End Sub
This comes pretty close to what I am looking for, but seems to move the paragraph up according to its position in the outline structure (what can become rather confusing). I just want to move it one paragraph/line up or down (also irrespective of its formatting).
(RStudio offers this nice feature where you can simply move selected text lines without copy-pasting; I am looking for the equivalent in word).
many thx.
The Relocate method is designed to work in Outline mode see here. Try the Move method instead:
Selection.Range.Move Unit:=wdParagraph, Count:=-1
You may need to adjust Count to get the effect you desire --- if -1 doesn't work, try -2, etc.
This would probably be cleaner using cut/paste but try this:
Sub Test_NewP()
Dim doc As Word.Document
Dim CurR As Word.Range
Dim NewP As Word.Paragraph
Dim IndexP As Long
Set doc = ActiveDocument
If doc.ActiveWindow.View = wdOutlineView Then
MsgBox "This program doesn't work in outline view --- please switch to another view", vbOKOnly, "Error"
Exit Sub
End If
Set CurR = Selection.Paragraphs(1).Range
IndexP = doc.Range(0, CurR.End).Paragraphs.Count
Set NewP = doc.Paragraphs.Add(doc.Paragraphs(IndexP - 1).Range)
NewP.Range.Text = CurR.Text
CurR.Delete
Set NewP = Nothing
Set CurR = Nothing
Set doc = Nothing
End Sub
This likely won't reliably manage formatting, but you could add code to fix that.
Hope that helps.

Add a content control after an existing content control in word 2010 using vba

A little more detail:
I am inserting (lots of) documents with content controls into a single document.
One of the content controls in each doc is a title control (linked to document property), which naturally receives the same value as the destination document's title on insert.
Renaming the control's title and or tag, using word or vba does not fix the problem (weird!)
My proposed solution is to create a new control with a different name, copy across the .range.text from the original title control and then delete the title control.
I have a loop which goes through all the files that need changing which works fine. However, whatever I seem to do, any new controls that I create appear at the beginning of the document and not in the correct place (there is a control with a code for the document before it).
Ideas? As an aside is there any logical reason why changing the control names doesn't work?
Current code:
Sub FieldChanger()
Dim docCur As Document
Dim strCurPath As String
Dim strCurFile As String
Dim rngTitle As Range
Dim strTitle As String
Dim ccName As ContentControl
strCurPath = "C:\Users\User\Desktop\BGS\Final\"
strCurFile = Dir(strCurPath & "*.docx")
Do While strCurrentFile <> ""
Set docCur = Application.Documents.Open(strCurPath & strCurFile)
With docCur.ContentControls
.Item(1).LockContents = False //Unlock outer content control
Set rngTitle = .Item(3).Range
strTitle = rngTitle.Text
rngTitle = rngTitle.Move(wdCharacter, 1)
ccName = rngTitle.ContentControls.Add(wdContentControlRichText) //This line throws a 4198 error
ccName.Title = "ccName"
ccName.Tag = "ccName"
ccName.Range = strTitle
ccName.LockContentControl = True
.Item(3).LockContentControl = False
.Item(3).Delete
.Item(1).LockContents = True //Lock outer content control
End With
docCur.Save
docCur.Close
strCurFile = Dir
Loop
End Sub
As an aside is there any logical reason why changing the control names doesn't work?
The Content Control (CC) name is just a name. Renaming the CC from "Title" doesn't change where Word gets the content from. Nor would naming a CC as "Title" cause Word to put the document's title string in the CC. If you create an empty document, insert the Title document property (as a CC) and look at the value of
activedocument.ContentControls(1).XMLMapping.XPath
you will probably see the value
/ns1:coreProperties[1]/ns0:title[1]
This is what tells Word that it needs to put the value of the Title builtin document property in the CC, and where to go to get it. You can link your own plain text CCs to builtin properties using the same mechanism, or you can link them to nodes in "Custom XML parts" of your own. But they don't have to be linked to anything.
As for the code, how about something more like this (NB, I have also changed "strCurrentFile" to strCurFile). I wondered whether you really need to re-insert the CC value as a new CC (i.e. why not just remove the CC and leave its existing value there) but have assumed that you need the CC there.
NB, as a general rule in VBA you need to use the Set keyword when setting the value of objects such as range variables and CCs. In theory you should also set objects to Nothing (e.g. Set rngTitle = Nothing) when you have finished with them. I haven't added that stuff here. In VB.NET you don't need to do either of those things.
Dim docCur As Document
Dim strCurPath As String
Dim strCurFile As String
Dim rngTitle As Range
Dim strTitle As String
Dim ccName As ContentControl
strCurPath = "C:\a\test\"
strCurFile = Dir(strCurPath & "*.docx")
Do While strCurFile <> ""
Set docCur = Application.Documents.Open(strCurPath & strCurFile)
With docCur.ContentControls
.Item(1).LockContents = False 'Unlock outer content control
Set rngTitle = .Item(3).Range
strTitle = rngTitle.Text
' we need the following line to ensure that deleting the range
' does not remove the CC prematurely
.Item(3).Temporary = False
rngTitle.Delete
rngTitle.Collapse wdCollapseStart
' Delete the control here instead of later
.Item(3).LockContentControl = False
.Item(3).Delete
Set ccName = rngTitle.ContentControls.Add(wdContentControlRichText)
ccName.Title = "ccName"
ccName.Tag = "ccName"
ccName.Range = strTitle
ccName.LockContentControl = True
.Item(1).LockContents = True 'Lock outer content control
End With
docCur.Save
docCur.Close
strCurFile = Dir
Loop
Comment consolidation...
There are addins that may help, e.g. the databinding toolkit at cctw.codeplex.com (not checked that link recently)