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

Rajesh Meena
Rajesh Meena Moderator, Employee Posts: 90
Second Anniversary 10 Comments 5 Answers Solution Developer Community of Practice Member
✭✭✭✭

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: 183
    100 Comments 5 Answers First Anniversary Ansys Employee
    admin

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

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 315
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited August 2023

    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, GitHub-issue-creator Posts: 315
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    Here's the original file.

  • James Derrick
    James Derrick Administrator, Employee Posts: 283
    Ancient Membership 100 Comments 100 Likes 25 Answers
    admin

    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.