Showing results 1 to 7 of 7

Thread: Creating Structure Datatypes and assign variables to a specific one

  1. #1
    mastr Gast

    Default Creating Structure Datatypes and assign variables to a specific one

    Hello,

    I want to create a structure datatype with VBA (this could also be done manually if the VBA solution is to complex).

    The following part is more important for me.

    As next step i want to search all variables for a certain string pattern, if i find one it should be put under the appropiate structure variable.

    Example:
    Structure:
    + engine
    ¦-- torque
    ¦-- power
    ¦-- engine displacement


    Example Variablelist:
    -somevar1
    -somevar2
    -myengine.power
    -otherengine.power
    -myengine.engine_displacement
    -somevar3
    -myengine.torque

    If my algorithm is invoked with a search parameter "myengine". Now the "flat variables" are put into the structure described above.

    Resulting structre:

    + myengine
    ¦-- myengine.torque
    ¦-- myengine.power
    ¦-- myengine.engine_displacement


    I think is it not possible with vba api to put an existing non-structure variable under a structure.
    Perhaps when i find a result i can create a new empty structre (if it not exists already). Then copy all values (name, limits, etc.) of the result variable to the corresponding part in the structure and delete the flat variable after copying all attributes.
    But i have no idea what i have to copy to create a new variable with exactly the same attributes.

    Please help me with my problem, i thank you in advance.

  2. #2
    Join Date
    30.05.2007
    Location
    Salzburg
    Posts
    880

    Default Re: Creating Structure Datatypes and assign variables to a specific one

    First of all please take a look into the VBA help documentation where many VBA examples about this topic are available (this documentation can be downloaded here: http://www.copadata.com/forums/showthread.php?t=37. Using zenOn version >= 6.22 the VBA help documentation is also available at the installation path of zenO ( i.e. "C:\Program Files\COPA-DATA\zenon 6.22 SP1\HELP\ENGLISH\VBA.chm")

    I want to create a structure datatype with VBA (this could also be done manually if the VBA solution is to complex).
    -> this is quite simple.... following VBA code creates a complex datatype called "Engine_Struct":

    Sub CreateComplexType()
    
    Dim obVarTypes As VarTypes
    Dim obVarType As VarType
    Dim obVarBaseType As VarType
    Dim obComplexItem As ComplexItem
    
        Set obVarTypes = MyWorkspace.ActiveDocument.VarTypes
        
        'set the base type (which datatyp the single subitems should be ... i.e. REAL)
        Set obVarBaseType = obVarTypes.Item("REAL")
        'create the complex item
        Set obVarType = obVarTypes.CreateComplexType("Engine_Struct")
        If Not obVarType Is Nothing Then
            'create the subitems (i.e. "torque", embedded datatype, Starting with offset 0)
            'check out the VBA help documenation at "VarType.NewSubItem" for the possible parameters (embedded, linked, offset,... )
            Set obComplexItem = obVarType.NewSubItem("torque", obVarBaseType, False, 0, 0, 0, 0)
            Set obComplexItem = obVarType.NewSubItem("power", obVarBaseType, False, 0, 0, 0, 0)
            Set obComplexItem = obVarType.NewSubItem("engine displacement", obVarBaseType, False, 0, 0, 0, 0)
        End If
    End Sub
    -> as mentioned in the comment within the VBA code:
    ==> check out the VBA help documenation at "VarType.NewSubItem" for the possible parameters (embedded, linked, offset,... )

    As next step i want to search all variables for a certain string pattern, if i find one it should be put under the appropiate structure variable.
    -> via VBA it is not possible to assign a other datatype to a existing variable (within the zenOn Editor it is possible)
    -> this choice is only available when creating the variable

    Find below the VBA code for creating a variable called "MyEngine" for which the created datatype "Engine_Struct" will be assigned:

    Sub createComplexVar()
    
    Dim obVariables As Variables
    Dim obVariable As Variable
    Dim obDrivers As Drivers
    Dim obDriver As Driver
    Dim obVarTypes As VarTypes
    Dim obVarType As VarType
    Dim iSubItems As Integer
    
        Set obDrivers = MyWorkspace.ActiveDocument.Drivers
        'set the used driver for the variable (i.e. Intern driver)
        Set obDriver = obDrivers.Item("Internal variables")
        'check if the driver exists
        If obDriver Is Nothing Then
            MsgBox ("Driver does not exist!")
            Exit Sub
        End If
        
        Set obVarTypes = MyWorkspace.ActiveDocument.VarTypes
        'set the vartype (Engine_Struct)
        Set obVarType = obVarTypes.Item("Engine_Struct")
        If obVarType Is Nothing Then
            MsgBox ("Vartype does not exist!")
            Exit Sub
        End If
        
        Set obVariables = MyWorkspace.ActiveDocument.Variables
        'create the variable with the complex datatype
        Set obVariable = obVariables.CreateVar("MyEngine", obDriver, 33, obVarType)
        'activate the SubItems
        obVariable.SubItemCount
        For iSubItems = 0 To obVariable.SubItemCount - 1
            obVariable.ActivateSubItem (iSubItems)
        Next iSubItems
    End Sub

    Regards,
    Herbert
    Last edited by herberto : 24th March 2009 at 11:05 Reason: forgot implementing "Exit Sub" in case of an error

  3. #3
    mastr Gast

    Default AW: Creating Structure Datatypes and assign variables to a specific one

    Thanks a lot!
    I will test your samples, and with them i hope i can handle the rest

  4. #4
    Join Date
    22.07.2009
    Posts
    6

    Default Re: Creating Structure Datatypes and assign variables to a specific one

    Hi,
    i have some additional questions regarding this topic:
    1.: How can i deactivate the automatic adressing (Hope this is the right term - german: Automatische Adressierung) when the bits of the Subitems are not continious?
    2.: If this is a struct witch is located in a datablock i have some trouble. I changed tpKanaltypes to 25, to 15 and to tpDataBlock, tpDatenbaustein. All ended up with nothing in the result.
    Thank you in advance!
    Michael

  5. #5
    Join Date
    15.11.2011
    Posts
    7

    Default AW: Re: Creating Structure Datatypes and assign variables to a specific one

    Hi,

    this topic is quite old, but i have the same problem. I want to create variables out of a structure with Address follows data type offset and start offset. How can i do this

    Wolfgang


    Quote Originally Posted by michael.grauer View Post
    Hi,
    i have some additional questions regarding this topic:
    1.: How can i deactivate the automatic adressing (Hope this is the right term - german: Automatische Adressierung) when the bits of the Subitems are not continious?
    2.: If this is a struct witch is located in a datablock i have some trouble. I changed tpKanaltypes to 25, to 15 and to tpDataBlock, tpDatenbaustein. All ended up with nothing in the result.
    Thank you in advance!
    Michael

  6. #6
    Join Date
    15.11.2011
    Posts
    7

    Default AW: Creating Structure Datatypes and assign variables to a specific one

    Hi.

    Now I can give the answer myself. With the new method "CreateArrayVarEx" (version 6.51 or higher) it is possible to adjust the type of adressing within the parameters. So it is possible to create structure variables and set it up properly.

    Wolfgang

  7. #7

    Daumen hoch Re: Creating Structure Datatypes and assign variables to a specific one

    thank you for posting the answer you found here

Similar Threads

  1. Replies: 0
    Last Post: 5th January 2009, 15:13
  2. Replies: 1
    Last Post: 12th June 2008, 14:56
  3. PLC Variables and Internal Variables
    By madphilly in forum Drivers
    Replies: 1
    Last Post: 4th June 2008, 06:59
  4. rapidly creating Zenon functions using import/export
    By mattsd in forum zenon Supervisor
    Replies: 3
    Last Post: 10th January 2008, 10:20

Posting Rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •