Is there any way to block a particular license and release when needed through scripting?

Options
Rajesh Meena
Rajesh Meena Moderator, Employee Posts: 67
First Anniversary Solution Developer Community of Practice Member Ansys Employee 5 Likes

I have a mixed wizard but there are limited licenses. I would like to block a particular license and release it before launching an application.

Is there any way to do that via script or subprocess using license client executables?

Tagged:

Answers

  • Chris Harrold
    Chris Harrold Member, Administrator, Employee Posts: 117
    First Answer First Comment First Anniversary Ansys Employee
    admin
    Options

    @AKD-Scripting-Team might have someone who knows, Rajesh.

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 208
    First Answer First Comment 5 Likes First Anniversary
    edited August 2023
    Options

    Here is a method to check for available licenses. I have used it to wait for a license to become available. I am not aware of a direct way to hold a license. But a workaround could be to open an instance of a product that pulls the license, and close it when needed.

    import System
    import clr
    clr.AddReference("Ans.Core.Components")
    import Ansys.Core.Components
    clr.AddReference("Ans.UI.Toolkit")
    clr.AddReference("Ans.UI.Toolkit.Base")
    import Ansys.UI.Toolkit
    clr.AddReference("Ans.Utilities")
    import Ansys.Utilities
    
    def licenseCheck(args):
        ExtAPI.Log.WriteMessage('Custom License Toolbar Button Clicked')
        #this snippet shows license validation when inside the running product
        wbCapability = Ansys.Core.Components.Validation.Capability.ANSYSLI_CAP_ANS_ACT
        isLicensed = Ansys.Core.Components.Validation.Validator.Check(wbCapability)
        mssg = "invalid"
        if isLicensed:
            mssg = "valid"
        Ansys.UI.Toolkit.MessageBox.Show(System.String.Format("Validator:  Capabiliy {0} is {1}!", str(wbCapability), mssg))
        #this snippet shows license validation when outside the running product
        installDir = ExtAPI.ExtensionManager.CurrentExtension.InstallDir
        licFilePath = System.IO.Path.Combine(installDir, "licenseoutput.xml")
        if System.IO.File.Exists(licFilePath):
            System.IO.File.Delete(licFilePath)
        awpRoot = Ansys.Utilities.ApplicationConfiguration.DefaultConfiguration.AwpRootEnvironmentVariableValue
        ansysFolder = System.IO.Path.Combine(awpRoot, "..")
        sharedFolder = System.IO.Path.Combine(ansysFolder, "Shared Files")
        licFolder = System.IO.Path.Combine(sharedFolder, "Licensing")
        licPlatformFolder = System.IO.Path.Combine(licFolder, "winx64")
        licutilPath = System.IO.Path.Combine(licPlatformFolder, "ansysli_util.exe")
        ExtAPI.ProcessUtils.Start(licutilPath, "-cap", str(wbCapability), "-getcapdef", licFilePath)
        mssg = "invalid"
        if System.IO.File.Exists(licFilePath):
            doc = System.Xml.XmlDocument()
            doc.Load(licFilePath)
            node = doc.SelectSingleNode("FEATURES")
            if node.ChildNodes.Count > 0:
                mssg = "valid"
            System.IO.File.Delete(licFilePath)
        Ansys.UI.Toolkit.MessageBox.Show(System.String.Format("LIC UTIL:  Capabiliy {0} is {1}!", str(wbCapability), mssg))
    
    

    Whitespace was not properly maintained when I copy/pasted (from Notepad++) @James Derrick please investigate.

    EDIT: I re-copy/pasted from VSCode per @James Derrick 's advice and it is now correct.

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 208
    First Answer First Comment 5 Likes First Anniversary
    Options

    Here's the original file.

  • James Derrick
    James Derrick Administrator, Employee Posts: 248
    First Anniversary Solution Developer Community of Practice Member Ansys Employee 5 Up Votes
    admin
    Options

    Regarding the code formatting, this seems to be a weakness of notepad and notepad++ as I do not see the same error with code copied from PyCharm or VS Code. I am not sure why exactly it is happening, although notepad++ is known for having issues when exporting code to other applications:

    https://stackoverflow.com/questions/3475790/copy-notepad-text-with-formatting

    https://community.notepad-plus-plus.org/topic/15791/paste-preserving-the-indentation-whitespace-conversion-issue/15

    https://community.notepad-plus-plus.org/topic/12678/copy-text-from-notepad-to-putty-terminal-loses-formatting/2

    It looks like notepad++ would need some configuration before it will work as you want it to.