Could anyone provide a script snippet showing how a .net abstract class is used in IronPython?
How do I use (override) its properties, methods, attributes?
Basically explaining this topic if possible in plain text.
Much obliged!
UPDATE:
Also getset_descriptors.
I always get TypeErrors when I try to use Abstract Class and getset_descriptors.
TypeError('Cannot create instances of SomeClass because it is abstract)
TypeError(‘getset_descriptor is not callable’,))
when I try SomeClass.__new__(OtherClass) I get TypeError(OtherClass does not define any public constructors)
etc.
here is an example with the abstract class of SpaceMorph.
I didn’t implement the Destructor (IDisposable), but its just for demonstration.
"""Provides a scripting component.
Inputs:
x: The x script variable
y: The y script variable
Output:
a: The a output variable"""
__author__ = "tomj"
import rhinoscriptsyntax as rs
import Rhino
from Rhino.Geometry import Vector3d,Point3d
class MyZMorpher(Rhino.Geometry.SpaceMorph):
def __init__(self):
Rhino.Geometry.SpaceMorph.__init__(self)
def Morph(self,geom):
geom.Translate(Vector3d(0,0,10))
def MorphPoint(self,point):
return Point3d(point.X,point.Y,-10)
"""
public abstract Point3d MorphPoint(
Point3d point)
"""
myMorpher = MyZMorpher()
if (isinstance(geom,Point3d)):
print "its a point"
geom = myMorpher.MorphPoint(geom)
else:
print "its not a point"
myMorpher.Morph(geom)
a = geom
What does __init__(self) do, exactly? Is this like __new__() creating an instance?
In the api I don’t see neither __init__ nor __new__ is this an example of “extended method” for a class?
EDIT:
One additional question. I understand that when using from ASSEMBLY import * all the namespaces inside are imported as modules. and when using from ASSEMBLY import SOMETHING this SOMETHING is imported as a namespace. What is the difference when importing .net stuff which one is preferable.
See I am not giving solid examples because I’m not using currently Rhino/GH Apis, I’m using another software that I would like eventually to connect with Rhino/GH. And I’m having trouble navigating through that other API. On top of all that I’m trying COM interop link which makes it even harder to figure out what’s what.
def __init__ (self) is the constructor of the class, whereas ParentClass.__init__(self) calls the constructor of the parent class. You can leave that out in this case , since an abstract class doesn’t do anything, its just a blue print.
Its just the basic way to derive in Python and IronPython 2.7
If you import everything from an module (import *) you can collide with names from other modules.
Depending on which module is loaded second, it will override the other. Better is “… import as …”
If you just need 2 classes, then you can directly import them with “from ASSEMBLY import SOMETHING”
Yeah, yeah, I know that.
What I was asking was the difference why are the namespaces imported as Modules when using import *
but when using from MODULE import NAMESPACE (that namespace is in fact .net dll)) then the namespace is imported as an actual namespace when you type(NAMESPACE) it.
My structure is i have a folder and I use it as a module, inside I have init.py and a bunch of other *.py that do clr.ReferenceFromFileAndPath(ALIEN.API.INTEROP.DLL)
This is just like C:\Users\peychev\AppData\Roaming\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\ghpythonlib
I don’t know, In C# you can compose different .dll into one, application-wide Namespace.
For instance the myapp.exe contains MYAPP.APP, whereas myapp.core.dll contains MYAPP.CORE and MYAPP.UTILS namespace
I have such case, ALIEN.API.INTEROP.DLL and ALIEN.API.INTEROP.UPGRADE.DLL have both the same namespace inside, and the same classes/interfaces/methods, the second one has a bit more. Should I import both as separate modules inside python, or as mentioned above, clr.Ref both then use import namespace only once?
I think both come from the same application, so concrete class definitions cannot be duplicated.
If you want the functionality ALIEN.API.INTEROP.UPGRADE.DLL just load it in as import ALIEN.API.INTEROP.UPGRADE as aaiu
if you need the stuff from ALIEN.API.INTEROP.DLL just load in import ALIEN.API.INTEROP as aai, but you cannot use ALIEN.API.INTEROP.UPGRADE.DLL objects if you are not loading in both. You can load in both as soons as they are part of the same application!
If they both coming from different apps then this is a problem, but its very unlikely.
and ALIEN.API.INTEROP.UPGRADE.DLL has
ALIEN.API.INTEROP.UPGRADE.Dolphin
ALIEN.API.INTEROP.UPGRADE.Elefant
you can only use Human if you load ALIEN.API.INTEROP.DLL, and if you need a Dolphin you can load in both or just ALIEN.API.INTEROP.UPGRADE.DLL. Usually you load all assemblies.
If you just load ALIEN.API.INTEROP.UPGRADE.DLL, ALIEN.API.INTEROP has no Human,Zebra and Rhino in there. Its that easy