Hi folks,
I’m having trouble using method overloading when using the CPython engine in Dynamo 2.10
Please check the following code to create a schema and place an instance of it in an element in the model.
It works just fine in IronPython but crashes in CPython (I skip the import block for brevity):
component = UnwrapElement(IN[0])
TransactionManager.Instance.EnsureInTransaction(doc)
def create_schema():
schema_guid = System.Guid("DF3BBCC1-4D4D-4A01-B444-F9722814F9CE")
schema_builder = SchemaBuilder(schema_guid)
schema_builder.SetReadAccessLevel(AccessLevel.Public)
schema_builder.SetWriteAccessLevel(AccessLevel.Public)
schema_builder.SetSchemaName("TestSchema")
schema_builder.SetDocumentation("Test schema to store ids")
field_builder = schema_builder.AddSimpleField("ParentComponentID", System.String)
schema = schema_builder.Finish()
return schema
def add_schema_instance(schema, rvt_element):
field = schema.GetField("ParentComponentID")
schema_instance = Entity(schema)
schema_instance.Set[System.String](field, "ID0004")
rvt_element.SetEntity(schema_instance)
TransactionManager.Instance.TransactionTaskDone()
schema = create_schema()
When I try to port it to CPython, this same code crashes with exception
Warning: TypeError : No method matches given arguments for Set: (<class ‘Autodesk.Revit.DB.ExtensibleStorage.Field’>, <class ‘str’>)
I have tried different permutations and no overloading like
schema_instance.Set(field, "ID0004")
But nothing seems to work…
What is the new syntax for method overloading when using CPython with Dynamo?
Thanks in advance and happy 2022 everyone!
I think you are trying to call a method with two parameters? I think your overload type list needs to include both types, the field and the string.
It’s also quite possible the overloading in pythonNet is just broken - I believe the approach has changed in the latest versions.
opened 12:58AM - 20 Jul 19 UTC
### Environment
- Pythonnet version: 2.4.0
- Python version: 3.6.5
- … Operating System: Windows 10
- Project .NET version: .NET Standard 2.0
### Details
I am attempting to use an overloaded method on an object with library-defined types (classes), and in the process of debugging why the method binder is unable to find the matching overload, I discovered that the method binder is not allowing me to access the overloads using the overload syntax. I tested this using primitive types to make sure that it was not a more complicated issue caused by the library or its types.
C# code:
```csharp
namespace LibSLH
{
public class OverloadTestClass
{
public static string ExampleStaticMethod(int foo)
{
return foo.ToString();
}
public static string ExampleStaticMethod(int foo, int bar)
{
return (foo + bar).ToString();
}
public static string ExampleStaticMethod(string bongo)
{
return bongo + " " + bongo;
}
public string ExampleInstanceMethod(int foo, int bar)
{
return (foo * bar).ToString();
}
public string ExampleInstanceMethod(int foo, int bar, string bongo)
{
return (foo * bar + " " + bongo);
}
}
}
```
Python code:
```python3
import clr
clr.AddReference('LibSLH')
from System import *
from LibSLH import OverloadTestClass
print(OverloadTestClass)
tester = OverloadTestClass()
print(tester)
print()
print('Overloads for OverloadTestClass.ExampleStaticMethod:')
print(OverloadTestClass.ExampleStaticMethod.Overloads)
print()
print('Overloads for OverloadTestClass.ExampleInstanceMethod, invoked on tester object:')
print(tester.ExampleInstanceMethod.Overloads)
print()
print('Tests:')
try:print(OverloadTestClass.ExampleStaticMethod[Int32](1))
except TypeError as e: print(e)
print(OverloadTestClass.ExampleStaticMethod(1))
print()
try:print(OverloadTestClass.ExampleStaticMethod[Int32, Int32](3, 4))
except TypeError as e: print(e)
print(OverloadTestClass.ExampleStaticMethod(3, 4))
print()
try:print(OverloadTestClass.ExampleStaticMethod[String]('henlo'))
except TypeError as e: print(e)
print(OverloadTestClass.ExampleStaticMethod("henlo"))
print()
try:print(tester.ExampleInstanceMethod[Int32, Int32](5, 5))
except TypeError as e: print(e)
print(tester.ExampleInstanceMethod(5, 5))
print()
try:print(tester.ExampleInstanceMethod[Int32, Int32, String](5, 5, 'equals 5 * 5'))
except TypeError as e: print(e)
print(tester.ExampleInstanceMethod(5, 5, 'equals 5 * 5'))
print()
```
Output:
```
<class 'LibSLH.OverloadTestClass'>
LibSLH.OverloadTestClass
Overloads for OverloadTestClass.ExampleStaticMethod:
System.String ExampleStaticMethod(Int32)
System.String ExampleStaticMethod(Int32, Int32)
System.String ExampleStaticMethod(System.String)
Overloads for OverloadTestClass.ExampleInstanceMethod, invoked on tester object:
System.String ExampleInstanceMethod(Int32, Int32)
System.String ExampleInstanceMethod(Int32, Int32, System.String)
Tests:
No match found for given type params
1
No match found for given type params
7
No match found for given type params
henlo henlo
No match found for given type params
25
No match found for given type params
25 equals 5 * 5
```
I am confused as to why the method binder is not matching the overloads when the type parameters are explicitly defined. Implicit overload calls are working in my example, but not always, so I want to specify them explicitly. I ran into problems while trying to compile the pythonnet CLR module, so I am using the distributed (`clr.pyd`) from the PyPi pythonnet package. This makes it hard for me to debug the method binder. Am I missing something, or is this a bug? Or is this expected behavior?
2 Likes
currently, it looks like PythonNet doesn’t work with template Parameter
opened 03:39PM - 10 Dec 21 UTC
on hold
## Problem
There's still no way to call methods with template parameters.
… - parabolic SAR
- ichimoku
## Related issue
- https://github.com/pythonnet/pythonnet/issues/1522
Thanks guys, I’ll stick to IronPython in the meantime
Any new of this? I have the same problem using the class “IFamilyLoadOptions”.
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit import DB
from Autodesk.Revit.DB.Structure import *
class FamilyLoaderOptionsHandler(DB.IFamilyLoadOptions):
def OnFamilyFound(self, familyInUse, overwriteParameterValues):
overwriteParameterValues = True
return True
def OnSharedFamilyFound(self, sharedFamily, familyInUse, source, overwriteParameterValues):
source = DB.FamilySource.Family
overwriteParameterValues = True
return True
fload_handler = FamilyLoaderOptionsHandler()
OUT=fload_handler
1 Like