Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the centroid of all elements of a group in VB.NET

Status
Not open for further replies.

rbas85

Structural
Sep 23, 2019
7
Dears, I'm trying to translate my vba for excel macro in VB.NET. Hear goes a snippet of my code in VB.NET:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim App As femap.model
App = GetObject(, "femap.model")
Dim cgXYZ As Object
Dim aux(3) As Double
Dim cgTXYZ(3) As Double
Dim PosIniModelX, Conversor, Mass, MassT, x, y, z As Double
Dim olel As femap.Set
olel = App.feSet
Dim el As femap.Elem
el = App.feElem
Dim elID, grID, rc As Long

grID = ReturnNumber(ComboBox1.Text)
rc = olel.AddGroup(femap.zDataType.FT_ELEM, grID)
Conversor = 1000
PosIniModelX = 0
elID = olel.First()
MassT = 0
Do While elID > 0
rc = el.Get(elID)
rc = el.GetFaceArea(0, Mass)
rc = el.GetCentroid(cgXYZ) <-- I'm having problems hear. Apparently is an object array, but I don't know how to declarate it
x = cgXYZ(0) / Conversor + PosIniModelX
y = cgXYZ(1) / Conversor
z = cgXYZ(2) / Conversor
MassT = MassT + Mass / 10000
aux(0) = aux(0) + x * Mass / 10000
aux(1) = aux(1) + y * Mass / 10000
aux(2) = aux(2) + z * Mass / 10000
elID = olel.Next()
Loop
MassT = MassT
aux(0) = aux(0) / MassT
aux(1) = aux(1) / MassT
aux(2) = aux(2) / MassT
cgTXYZ(0) = aux(0)
cgTXYZ(1) = aux(1)
cgTXYZ(2) = aux(2)
End Sub
 
Replies continue below

Recommended for you

Try to dim it as a variant not an object.

"Dim cgXYZ as Variant"
 
To clarify, which way are you converting to? Are you moving the code into Excel VBA or are you createing a vb.net exe in an IDE like Visual Studio.

If it's into excel the comment above should apply.

If it's into a vb.net exe then I believe Dim cgXYZ as Object is fine. This problem might be when looping through elements it is trying to overwrite the object and crashing there. Try adding " cgXYZ = Nothing " right under the Do While elID > 0. This will clear the object first.

Do While elID > 0

cgXYZ = Nothing

rc = el.Get(elID)
rc = el.GetFaceArea(0, Mass)
rc = el.GetCentroid(cgXYZ) '< --I 'm having problems hear. Apparently is an object array, but I don't know how to declarate it
x = cgXYZ(0) / Conversor + PosIniModelX
y = cgXYZ(1) / Conversor
z = cgXYZ(2) / Conversor
MassT = MassT + Mass / 10000
aux(0) = aux(0) + x * Mass / 10000
aux(1) = aux(1) + y * Mass / 10000
aux(2) = aux(2) + z * Mass / 10000
elID = olel.Next()
Loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor