Error: attribute 'CurrentUnitFromQuantityName' of 'namespace#' object is read-only

ZZ.tang
ZZ.tang Member Posts: 17
Name Dropper First Comment
**

ANSYS version: 2021r2 & 2023r1 mechanical

I'm trying to build an ACT APP extension in mechanical, when I tried to run it below warning shows up.

attribute 'CurrentUnitFromQuantityName' of 'namespace#' object is read-only 
MissingMemberException 
at IronPython.Runtime.Binding.PythonGetMemberBinder.NamespaceTrackerDelegate.Target(CallSite site, Object self, CodeContext context) 
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) 
at Microsoft.Scripting.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame) 
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1) 
at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx) 
at Ansys.ACT.Core.Extension.ReadScripts() 
Extension NamedSelection_Toolbox loaded for context Mechanical.

Then I tried to modified the code, then I noticed if I just add a line like below, without any operation, the warning still shows up.

DataModel.CurrentUnitFromQuantityName

And it very strange that the same code could be run normally in mechanical scripting without any warning.

Best Answer

  • AlexGh
    AlexGh Member Posts: 23
    10 Comments First Answer Name Dropper
    **
    Answer ✓

    Try accessing the function CurrentUnitFromQuantityName via

    ExtAPI.DataModel.CurrentUnitFromQuantityName.
    

    Which version of Ansys are you using?

Answers

  • AlexGh
    AlexGh Member Posts: 23
    10 Comments First Answer Name Dropper
    **

    Hi ZZ.tang!
    The message 'attribute 'CurrentUnitFromQuantityName' of 'namespace#' object is read-only' means that the requested attribute is missing from the object. This can happen, for example, when using an extension developed for a different version of Ansys. Developers may remove or relocate certain attributes and functions of objects between Ansys versions. Could you provide a bit more context? Perhaps part of the code that gives this error?

  • ZZ.tang
    ZZ.tang Member Posts: 17
    Name Dropper First Comment
    **
    import clr
    clr.AddReference("Ans.Utilities")
    clr.AddReference('Ans.UI.Toolkit')
    clr.AddReference('Ans.UI.Toolkit.Base')
    clr.AddReference('System.Windows.Forms')
    clr.AddReference('System.Drawing')
    clr.AddReference('System.IO')
    clr.AddReference("Ansys.Mechanical.DataModel")
    
    import Ansys.Mechanical.DataModel as DataModel
    
    DataModel.CurrentUnitFromQuantityName
    

    Something like this cannot work in ACT APP extension
    but can work in scripting

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 295
    25 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    import Ansys.Mechanical.DataModel as DataModel
    ^--This is not the proper way to define DataModel. Instead do this:
    DataModel = ExtAPI.DataModel

  • ZZ.tang
    ZZ.tang Member Posts: 17
    Name Dropper First Comment
    **

    @ZZ.tang said:

    import clr
    clr.AddReference("Ans.Utilities")
    clr.AddReference('Ans.UI.Toolkit')
    clr.AddReference('Ans.UI.Toolkit.Base')
    clr.AddReference('System.Windows.Forms')
    clr.AddReference('System.Drawing')
    clr.AddReference('System.IO')
    clr.AddReference("Ansys.Mechanical.DataModel")
    
    import Ansys.Mechanical.DataModel as DataModel
    
    DataModel.CurrentUnitFromQuantityName
    

    Something like this cannot work in ACT APP extension
    but can work in scripting

    sorry, I just checked
    the error might caused by "import Ansys.Mechanical.DataModel as DataModel"
    if I run

    import Ansys.Mechanical.DataModel as DataModel
    DataModel.CurrentUnitFromQuantityName
    

    and then
    it also not work in scripting

    but

    ExtAPI.DataModel.CurrentUnitFromQuantityName 
    

    works both in APP and scripting