I have a user who needs easy access to a drop down list of macros that will easily and quickly import a general table template and will let them place it where they needed it on a cursor click. Currently, the code I have for importing the general table works but I am not familiar enough with the solid works parameters to know which argument to pass to allow the table to be pasted where the user chooses. The specific line in question is Set swTable = swDrawing.InsertTableAnnotation2(True, 0, 0, swBOMConfigurationAnchor_TopLeft, MATABLE, 2, 1)
The full code is as follows;
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDrawing As SldWorks.DrawingDoc
Dim swTable As SldWorks.TableAnnotation
Const MATABLE As String = "C:\STANDARD Tables\sampleTable.sldtbt"
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If (swModel Is Nothing) Or (swModel.GetType <> swDocDRAWING) Then
swApp.SendMsgToUser ("To be used for drawings only, Open a drawing first and then TRY!")
Exit Sub
End If
Set swDrawing = swModel
Set swTable = swDrawing.InsertTableAnnotation2(True, 0, 0, swBOMConfigurationAnchor_TopLeft, MATABLE, 2, 1)
If Not swTable Is Nothing Then
swTable.BorderLineWeight = 0
swTable.GridLineWeight = 0
End If
End Sub
Thank you.
The second and third parameters (where you have 0 and 0) are used to set the position of the table, but you must also set the first parameter to False (which instructs the API to ignore the anchor and use your X and Y coordinates). See http://help.solidworks.com/2020/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IDrawingDoc~InsertTableAnnotation2.html
So your line would be, for example:
Set swTable = swDrawing.InsertTableAnnotation2(False, 10, 10, swBOMConfigurationAnchor_TopLeft, MATABLE, 2, 1).
However, a couple of points with your code
Firstly, you'll need to load the document into your VBA Solidworks app object. Your code, as written, won't work because swApp won't have an active document. The best way of doing this would be to offer a file dialog box and load the selected file into the app, using the OpenDoc6() function. https://help.solidworks.com/2019/english/api/sldworksapi/Open_Document_Example_VB.htm
Secondly, getting the mouse position and converting that to the X and Y parameters for your InsetTableAnnotation2() function will require knowledge of some WinAPIs and a bit of pixel calculation. It's perfectly doable but there's a bit of coding involved. If, on the other hand, you set the swApp.Visible to true, then when the table has successfully inserted itself into the Solidworks document, it will be visible to the user and can be dragged around as he/she wishes. You might find this is an easier solution for you.
As an aside, Solidworks is quite strict on licensing and number of users. You would certainly only want one instance of swApp, so declare that at module level, but you would probably also need to check for an error in the event that number of licences are exceeded.
Related
I have an CATIA assembly created automatically by VBA Excel. Now I need to add 6 same bolts to that, for example. Theoretically, I can add some pieces of VBA code to create each bolt in that assembly as the separate one, which seems to be so cumbersome. Is it possible to create a bolt (not saved yet, because I want to see how it is in 3D CAITA environment first) and make copies of it as the others in the assembly, like the way we often do interactively in CATIA. If possilbe, please tell me how to do that. Many thanks!
If I understand your request correctly, you would like to have 6 copies of the not yet saved CATPart in the Assembly.
You can achieve this with a simple copy/paste.
For example:
Sub CATMain()
Dim oDoc As Document
Dim oSel As Object
Dim aFilter(0)
Dim oSelected
Dim oRef As Reference
Set oDoc = CATIA.ActiveDocument
Set oSel = oDoc.Selection
oSel.Clear
aFilter(0) = "Product"
oSelected = oSel.SelectElement2(aFilter, "Select Object in the tree which you want to insert six times or ESC..", True)
If oSelected = "Cancel" Then
Debug.Print "KO"
Exit Sub
End If
oSel.Copy
Dim i As Integer
For i = 0 To 6
oSel.Clear
oSel.Add oDoc.Product
oSel.Paste
Next
End Sub
You can of course replace the system to pick product you want to copy to the automatic way, depend on your demanded logic.
I'd like to do what feels like a fairly simple task, and I've found the specific API Help pages which should make it clear, but, I can't actually make things work.
The Key steps that I would like to achieve are:
Rename the active document
Update References to this document to accommodate new name
Save active document.
This help page shows the Usage for renaming the doc, and under the "Remarks" heading, includes links to the next two steps, mentioning them off hand as if implementing them would be easy.
https://help.solidworks.com/2020/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDocExtension~RenameDocument.html?verRedirect=1
The trouble is, I'm a bit of a VBA beginner - usually I get by with the 'record' function, and then tidying things up from there - but undertaking the steps above manually doesn't result in anything being recorded at all for one reason or another.
Assuming I am able to pass in the item to be renamed (I'll define a variable at the start of the Sub for this e.g. swModel = swApp.ActiveDoc), and the new name (NewName = "NEW NAME HERE"), How would I translate the Help API into a Sub that I can actually run?
Two of them suggest declaring as a Function, and one as a Public Interface - I've never used these before - do these just run in a standard Module? Do I need to write a 'master Sub' to call the different functions sequentially, or could these be included directly in the sub, if they're only to be used once?
[Feeling a little lost - it's demoralizing when the help files aren't all that helpful]
Let me know if there's any more information missing that I can add to improve my question - as I said, I'm fairly new to this coding thing...
The "record" function is sometimes a good point to start but there are a lot of functions it can't recognize while you execute them manually.
The API Help is then useful to find out how to use a specific function.
In almost every example the use of a specific method (e.g. RenameDocument) is only shown abstract. There is always a instance variable which shows you the object-type needed to call this method. So you can use these in every sub you want, but beforehand need access to the specific instance objects.
For your example the RenameDocument method is called with an object of the type IModelDocExtension. First thing for you to do is to get this object and then you can call the method as described in the help article.
Under Remarks in the article you find additional information for what you maybe have to do before or after calling a method.
For your example it is mentioned that the renaming takes permanently place after saving the document.
And finally here is what you want to do with some VBA code:
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Sub main()
' get the solidworks application object
Set swApp = Application.SldWorks
'get the current opened document object
Set swModel = swApp.ActiveDoc
' get the modeldocextension object
Dim swModelExtension As ModelDocExtension
Set swModelExtension = swModel.Extension
Dim lRet As Long
lRet = swModelExtension.RenameDocument("NEW NAME")
If lRet = swRenameDocumentError_e.swRenameDocumentError_None Then
MsgBox "success renaming"
Else
MsgBox "failed with error: " & lRet
End If
End Sub
Afterwars you have to process the return value to check for errors described in this article: https://help.solidworks.com/2020/English/api/swconst/SolidWorks.Interop.swconst~SolidWorks.Interop.swconst.swRenameDocumentError_e.html
I have a CATIA macro in VBA, that draws points by coordinates (from arrays).
It works on my computer (Catia V5-R2014 and on my neigbours - two versions V5-R2014 and R21).
But it doesn't work for colleges in a different city (they have version R21).
Basically, my macro reads input data from file, calculates coordinates, writes them in out-file, and then draws these points.
All steps except the last one work on either computer/version.
But at the last step "their" Catia just doesn't plot anything, w/o any errors.
So the Subruotine for the last step is:
Sub PlotGeometry()
' Nmlp - number of points
Dim i As Integer
Dim oPartDocument As Document
Dim ohSPointCoord() As HybridShapePointCoord
Dim ohSPoints As HybridShapePointCoord
Dim bodies1 As Bodies
Dim body1 As Body
ReDim ohSPointCoord(0 To Nmlp)
Set oPartDocument = CATIA.Documents.Add("Part")
Set oPart = oPartDocument.Part
Set oPartBody = oPart.MainBody
Set oPlaneYZ = oPart.CreateReferenceFromGeometry(oPart.OriginElements.PlaneYZ)
' -- Draw Points
Dim ohSFactory As HybridShapeFactory
Set ohSFactory = oPart.HybridShapeFactory
For i = 0 To Nmlp
Set ohSPointCoord(i) = ohSFactory.AddNewPointCoord(XM(i), YM(i), ZM(i))
oPartBody.InsertHybridShape ohSPointCoord(i)
Next i
oPart.Update
End Sub
What can it be?
Perhaps at your site you have Hybrid Design enabled, and at the other site they do not.
With Hybrid Design enabled, you would be able to add points to a Body. Not so if it is not enabled and you would get no error from your code.
The setting is under Tools->Options->Infrastructure->Part Infrastructure->Part Document Tab->Enable hybrid design inside part bodies and bodies.
For unexplained reasons, hybrid design being enabled is the default. However I do not recommend using it.
If you just want to make your code work in both places then use a Geometrical Set to aggregate your points instead of the main body.
Dim pointsBody as HybridBody
Set pointsBody = oPart.HybridBodies.Add
pointsBody.Name = "Points_Body"
...
For i = 0 To Nmlp
Set ohSPointCoord(i) = ohSFactory.AddNewPointCoord(XM(i), YM(i), ZM(i))
pointsBody.AppendHybridShape ohSPointCoord(i)
Next i
Just a random guess:
Go to VBE>Tools>References
and compare the values from both computers. They should be identical.
Compare these checkboxes:
If they are different, make sure to make them identical to the PC that works.
I'm busy with some word automation and have run into an issue whereby a context menu within a document has items in, that I wish to remove.
Once the document is open, through vba I can remove these items by running the following code;
[VBA]
Dim oContextMenu As CommandBar
Dim oContextMenuItem As CommandBarControl
'Make changes to the ActiceDocument only (this is needed to make any changes to this document).
CustomizationContext = ActiveDocument
For Each oContextMenu In ActiveDocument.CommandBars
If oContextMenu.Type = MsoBarType.msoBarTypePopup Then 'Loop through all the context menus of type (msoBarTypePopup)
For Each oContextMenuItem In oContextMenu.Controls
If (InStr(oContextMenuItem.Caption, "Smokeball")) Then
oContextMenuItem.Delete
End If
Next
End If
Next
If I execute this code and check the document, all contextMenu sub items that contain the text "smokeball" are removed.
When I try move this code to my VB.NET solution (I have no choice of language, so VB it is), I get errors on the CustomizationContext = ActiveDocument line (this line has to be there for it to affect the current document).
The error I get is CustomizationContext' is not a by reference property.
Does anyone know how to get just that ONE line equivalent for vb.net?
Thanks in advance.
EDIT: In case you need to see the vb.net sub:
Private Sub RemoveUnwantedContextMenuItems()
Dim oContextMenu As CommandBar
Dim oContextMenuItem As CommandBarControl
'Make changes to the ActiceDocument only (this is needed to make any changes to this document).
WordApplication.CustomizationContext = WordApplication.ActiveDocument 'This is the error.
For Each oContextMenu In WordApplication.CommandBars
If oContextMenu.Type = MsoBarType.msoBarTypePopup Then 'Loop through all the context menus of type (msoBarTypePopup)
For Each oContextMenuItem In oContextMenu.Controls
If (InStr(oContextMenuItem.Caption, "Smokeball")) Then
oContextMenuItem.Delete()
End If
Next
End If
Next
End Sub
PS - I have also already tried using the .AttachedTemplate as well as .Normal / .NormalTemplate
Jules pointed me in the right direction with his sample code.
After lots of playing around I noticed that somewhere in the solution, the [TYPE] of WordApplication was getting changed to a dynamic type of sorts, hence, it couldn't use CustomizationContext.
My solution was this:
I changed this line;
WordApplication.CustomizationContext = WordApplication.ActiveDocument
To this:
CType(WordApplication, Microsoft.Office.Interop.Word.Application).CustomizationContext = WordApplication.ActiveDocument
Forcing the types to be correct.
Simple solution but took some time.
Thanks to Jules for pointing me in the right direction.
(Points should go to you).
I am trying to edit the part dimension in CATIA by changing the dimension values in the design table in the excel file. Everytime when i change the values i should manually update the part model. I want to update it automatically through VBA code and save it.
I tried
Sub CATMain()
CATIA.DisplayFileAlerts = False
Dim part As PartDocument
Set part = CATIA.ActiveDocument
part.Update
part.SaveAs "D:\E\CSE\.....\Part2.CATPart"
End Sub
and it is not working.
How can we update and save it??
You called ".Update" on the Document object, not the Part-object!
Answer is:
Dim part As PartDocument
Set partDoc = CATIA.ActiveDocument
partDoc.Part.Update
Your Code is correct and it should be working. Do you get any errors?
If you simply want an automatic update try to change your settings ...
Goto Tools->Options Then in the options dialog, goto Infrastructure Tree Node, and expand that and goto Part Infrastructure. Now on the right pane in the General Tab, Make sure you select Automatic for the Updates.
If this doesn't work you could try to the Part.UpdateObject objectToUpdate method to update the individual feature(s) that need to be updated.