AnsweredAssumed Answered

GetEntityByName: how to use it in assemblies?

Question asked by Federico Bassi on Jul 2, 2020
Latest reply on Jul 6, 2020 by Federico Bassi

Hi everyone,


Yesterday I have developed a simple (and working) macro to print a .txt file with the average displacement results of a frequency simulation of a solid part.

The printed averaged displacements are the one referred to a face previously named "Brg1".

This macro works fine with SW parts however when I am trying to print the same data with a SW assembly file I get the following error:

"Run-time error '438': Object doesn't support this property or method"

associated with this line: "Set swEntityFace = swPart.GetEntityByName("Brg1", 2)" 

It's clear to me that I can't reach the "Brg1" face with swPart in an assembly.
So the question is: how can I get the reference to "Brg1" face in an assembly?


Thank you very much for your help,



Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swPart As SldWorks.ModelDoc2

Sub main()

Dim swApp As SldWorks.SldWorks
Dim CWAddinCallBack As CosmosWorksLib.CWAddinCallBack
Dim ActDoc As CosmosWorksLib.CWModelDoc
Dim StudyMngr As CosmosWorksLib.CWStudyManager
Dim Study As CosmosWorksLib.CWStudy
Dim CWMesh As CosmosWorksLib.CWMesh
Dim CWResult As CosmosWorksLib.CWResults
Dim PostPlot As CosmosWorksLib.CWPlot
Dim swEntityFace As SldWorks.Face2
Dim swEntityArray As Variant
Dim CWObject As Object
Dim Disp() As Variant
Dim x, H, V As Integer
Dim i As Integer
Dim j As Integer
Dim DispAvg() As Single
Dim DispTemp As Single
Dim errCode As Long

If swApp Is Nothing Then Set swApp = Application.SldWorks
Set swPart = swApp.ActiveDoc
Set CWObject = swApp.GetAddInObject("SldWorks.Simulation")
Set CWAddinCallBack = swApp.GetAddInObject("CosmosWorks.CosmosWorks")
If CWAddinCallBack Is Nothing Then ErrorMsg swApp, "No CWAddinCallBack object", True
If COSMOSWORKS Is Nothing Then ErrorMsg swApp, "No CosmosWorks object", True
Set ActDoc = COSMOSWORKS.ActiveDoc()
If ActDoc Is Nothing Then ErrorMsg swApp, "No active document", True

'Get Ready study
Set StudyMngr = ActDoc.StudyManager()
If StudyMngr Is Nothing Then ErrorMsg swApp, "No study manager object", True
Set Study = StudyMngr.GetStudy(StudyMngr.ActiveStudy)
If Study Is Nothing Then ErrorMsg swApp, "No study object", True
'cwResullts now gives you access to all the results you can get interactively
Set CWResult = Study.Results

H = 0
V = 1

'Disp Brg1H
ReDim DispAvg(15, 1)
Set swEntityFace = swPart.GetEntityByName("Brg1", 2)
swEntityArray = Array(swEntityFace)
For i = 1 To 15
Disp = CWResult.GetDisplacementForEntities(H, i, Nothing, swEntityArray, 0, errCode)
'Avg Disp calculation
x = UBound(Disp, 1) - LBound(Disp, 1) + 1
x = x / 2
DispTemp = 0
For j = 0 To x - 1
DispTemp = DispTemp + Disp(j * 2 + 1)
Next j
DispTemp = DispTemp / x
DispAvg(i, 1) = DispTemp
Next i


Dim strFile_Path As String
strFile_Path = "C:\Users\f.bassi\Desktop\test.txt" 'Change as per your test folder path
Open strFile_Path For Output As #1
For j = 1 To 15
Print #1, DispAvg(j, 1)
Next j
Close #1

End Sub