Hello,
I want to create a class with a number of related properties which are calculated inside the class when certain property is assigned but I can not make it…
python code below is a simplified version of my class, which does not work as expected
two values displayed on the end should not be the same, but they are…
what am I doing wrong ?
thanks in advance…
import rhinoscriptsyntax as rs
class Test:
def __init__(self,obj=None):
self.Object=obj
self.Volume=0.0
self.m3=0.0
def Volume(self):
return self.Volume
def Volume(self,x):
self.Volume=x
self.m3=x/1000000000
def m3(self):
return self.m3
def m3():
obj = rs.GetObject()
T = Test(obj)
vol = rs.SurfaceVolume(obj)[0]
T.Volume =vol
print vol
print T.m3
return
m3()
dale
(Dale Fugier)
March 6, 2021, 12:55am
2
Hi @AleksandarSM ,
Maybe something like this?
import Rhino
class VolumeHelper(object):
def __init__(self, obj = None):
self.m_object = obj
self.m_volume = Rhino.RhinoMath.UnsetValue
self.m_m3 = Rhino.RhinoMath.UnsetValue
def IsValid(self):
rc = isinstance(self.m_object, Rhino.DocObjects.RhinoObject)
if rc:
rc = Rhino.RhinoMath.IsValidDouble(self.m_volume)
if rc:
rc = Rhino.RhinoMath.IsValidDouble(self.m_m3)
return rc
@property
def Volume(self):
return self.m_volume
@Volume.setter
def Volume(self, val):
self.m_volume = val
self.m_m3 = val / 1000000000
@property
def M3(self):
return self.m_m3
def TestVolumeHelper():
filter = Rhino.DocObjects.ObjectType.PolysrfFilter
rc, objref = Rhino.Input.RhinoGet.GetOneObject("Select closed polysurface", False, filter)
if not objref or rc != Rhino.Commands.Result.Success: return rc
obj = objref.Object()
if not obj: return
brep = objref.Brep()
if not brep: return
if not brep.IsSolid: return
volume_helper = VolumeHelper(obj)
volume_helper.Volume = brep.GetVolume()
if volume_helper.IsValid:
print(volume_helper.Volume)
print(volume_helper.M3)
if __name__ == "__main__":
TestVolumeHelper()
– Dale
Hello Dale,
I tried your example but it does not calculate the M3 property correctly., similar to mine
This time I get M3 value -1.23432101234e+308 regardless of the selected solid, which is the default value for the property
BR
Aleksandar
Continuing the discussion from Storing calculations inside class with python :
Thanks a lot!
I didn’t understand at first, but then then I found this article on the subject:
https://stackoverflow.com/questions/42258224/difference-between-class-foo-class-foo-and-class-fooobject
and it is all clear now. - in Rhino we have (Iron)Python 2.xx…
BR
Aleksandar
1 Like