This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Automatic Drawing Naming Standard

Former Member
Former Member
Hello! I just got m-files up and running, so I'm fairly new the VBscript on it. I'm hoping someone could help me out with my code.

I would like to automatically name all drawings in the class "Drawings" that are inserted based on two properties and an auto number.
Property 1: Drawing type (value list consisting of "ELE", "MEC", "CIV", "STR", etc.)
Property 2: Project (choose from list of existing Projects)
Auto-number: Counter which increments when documents by looking at other drawings of the same project having the same Property 1 & Property 2

For Example
Drawing 1
Drawing Type: ELE
Project: PARKING

=> Output: PARKING-ELE-01

Drawing 2
Drawing Type: ELE
Project: PARKING

=> Output: PARKING-ELE-02

Drawing 3
Drawing Type: MEC
Project: PARKING

=> Output: PARKING-MEC-01

And so on. So since a Document 1 exists with "PARKING" as a project and "ELE" as a drawing type, then Document 2 gets incremented by 1.

Is there anyone who has done something similar? I would really appreciate it if I could get some guidance with coding the Calculated Value (VBscript).

Thanks!

  • Former Member
    Former Member
    Hi ayhoom, look at this topic http://community.m-files.com/index.php?topic=520.0 you will find similar vbcode to solve your problem
    BR Lele (codbit)
  • Former Member
    Former Member
    Hi ayhoom,

    I have done something similar with what we refer to as "Bucket Documents". They are basically pools of documents that sit under one class and are sub-categorised by document type. This would be similar to what you are trying to achieve.

    I was going down the path of using the project # in the title of a document and it soon developed in to a problem with the lenght of the file names becoming too long and then the issue of 256 character limitation within Windows causes and issue and files seem to disappear, even though they are there. I found it best to set up common views that group the files by properties such as project. If you are trying to use the M-files properties as fields in your documents for titles or something similar, it's easy enough to just place two fields instead of one in the document.

    Anyway, this is the code that I have managed to get working the way I want with the help of some of the great people on this forum. I too am a relative beginner when it comes to VBS, but I have managed to get this working, I'm sure some of the experts on here could simplify my code down if necessary :)

    Enjoy your M-Files journey!


    Option Explicit
    Dim dtBucketTitle 'get the current title of the document if it exists.
    dtBucketTitle = PropertyValues.SearchForProperty ( 1043 ).TypedValue.GetValueAsLocalizedText ()
    Dim index
    index = InStr(dtBucketTitle,"-") 'find the position of the leading characters of the file
    If index > 0 then
    Dim szTest
    szTest = Left(dtBucketTitle,3) 'check to see if it is a valid "Bucket document" currently by the prefix
    If szTest = "DRW" or szTest = "DXO" or szTest = "EBT" or szTest = "FRM" or szTest = "HAZ" or szTest= "TMP" or szTest = "TRG" or szTest = "WI " then
    Dim dtDocName1
    dtDocName1 = PropertyValues.SearchForProperty ( 1038 ).TypedValue.GetValueAsLocalizedText ()
    Dim dtDocNum
    dtDocNum = PropertyValues.SearchForProperty ( 1037 ).TypedValue.GetValueAsLocalizedText ()
    Output = dtDocNum & ": " & dtDocName1 're-concatenate the current document # and name to give the full file name.
    Else
    Call GenerateNo 'create a new document number and name if above conditions are not met
    End If
    Else
    Call GenerateNo 'create a new document number and name if nothing exists
    End If
    '-----------------------------------------------------------------
    Sub GenerateNo

    Dim dtDocName 'get the document name
    dtDocName = PropertyValues.SearchForProperty ( 1038 ).TypedValue.GetValueAsLocalizedText ()
    Dim dtDocType 'get the document type
    dtDocType = PropertyValues.SearchForProperty ( 1034 ).TypedValue.GetValueAsLocalizedText ()
    Dim dtCount 'the following 3 steps sets up parts of the name for the vaultsharedvariable by finding the leading characters, and section#
    dtCount = InStr(dtDocType,"-")
    Dim dtAbbrDocType
    dtAbbrDocType = Left(dtDocType,dtCount-2)
    Dim dtSectNo
    dtSectNo = "S" & Left(PropertyValues.SearchForProperty ( 1028 ). TypedValue.GetValueAsLocalizedText,2)
    Dim Counter 'set the full vaultsharedvariable name
    Counter = dtAbbrDocType & dtSectNo
    Dim nnn,SectCounter 'set the counter to the current vaultsharedvariable value
    SectCounter = VaultSharedVariables(Counter)
    'the counter sets to 1 if this is the first time this vaultshared variable is used (3 digits set below in output)
    IF IsNull(SectCounter) or IsEmpty(SectCounter) or SectCounter = "" Then
    nnn=1
    Else
    nnn = CInt(Right(SectCounter,3)) + 1 'increment counter by 1 for a new document
    End IF
    VaultSharedVariables (Counter) = nnn 'store the current count in the vault shared variable
    'concatenate the document type, the document section #, and the 3 digits for the # count followed by the document name i.e. FRM-S01-001: Test Form
    Output = dtAbbrDocType & "-" & dtSectNo & "-" & Right ("000" & nnn,3) & ": " & dtDocName
    End Sub
  • Former Member
    Former Member
    Ahhhhh codbit, you are just too fast for me! ;)

    ayhoom, codbit has been a great help in the past for me, listen to his wise advice.
  • Former Member
    Former Member
    Wow! That was fast! Thank you so much for all your help :) I appreciate it.

    Soon enough, I hope I can start contributing on the M-Files community.

    I will post my code when its up and running

    Cheers!
  • Former Member
    Former Member
    Hello!

    Thank you for helping me with by redirecting me to a similar post. I'm stuck with one more thing, so I'm resorting to your help. I have attached a simple structure to illustrate the hierarchy.

    When creating a new "Drawing" class, I would like the "Drawing Name" to automatically be calculated using VB based on the selection of the "Project" (ID 1016), "Drawing Type" (ID1020), "Drawing Size" (1021) and give an autonumber at the end in case different file with same selection exists.

    I am stuck with looking up the "Project Number" (ID 1017) based on the selection of "Project" (ID 1016).
    So far, I have the following code. I've looked through the forum with no answer. Could you please guide me? I appreciate your help

    Option Explicit

    'Extract current value of the Project property
    Dim cntname
    cntname = PropertyValues.SearchForProperty( 1016 ).TypedValue.DisplayValue

    'MISSING CODE: Want to lookup up automatically assigned "Project Number" (ID 1017), based on "Prject" (ID 1016)
    Dim projnum
    projnum =


    Dim drwtyp, drwsize
    drwtyp = PropertyValues.SearchForProperty( 1020 ).TypedValue.DisplayValue
    drwsize = PropertyValues.SearchForProperty( 1021 ).TypedValue.DisplayValue

    'custom numbering using VaultSharedVariables to store value for each Project
    Dim n, v
    v = VaultSharedVariables(cntname)
    If isNull(v) or isEmpty(v) or v="" Then
    'case when first document of current project was created
    n = 1
    Else
    'increment counter in case when project was just handle
    n = CInt(v)+1
    End If

    'store counter in VaultSharedVaribles
    VaultSharedVariables(cntname) = n

    Output = drwsize & "-" & projnum & "-" & drwtyp & "-" & n


  • Former Member
    Former Member
    Hi ayhoom I prefer to post the response here to help anybody here in the community with the "same" problems...

    First of all I think that you want an autonumber that depend on Project + DrawingType + DrawingSize (correct me if I wrong) so you have to change the code that set [tt]cntname[/tt] because with that you wrote you build a counter based only to Project not on the other 2 properties!

    This maybe the correct code, and must be set after reading the 2 variables [tt]drwtyp[/tt] and [tt]drwsize[/tt]
    Dim prjname, cntname 
    prjname = PropertyValues.SearchForProperty( 1016 ).TypedValue.DisplayValue
    'here insert your correct code to read drwtyp and drwsize
    cntname = prjname & "|" & drwtyp & "|" & drwsize


    And than your main question about reading a property of another object based on the ID selected in a multi-select lookup property (as Project (1016) is) - for handle this you must use the method Vault.ObjectOperations.GetLatestObjectVersionAndProperties see more on this on M-Files API Documentation!

    Here is your "MISSING CODE"

    Dim projnum

    'read the ID of the selected project
    Dim multiIDs '
    Set multiIDs = PropertyValues.SearchForProperty( 1016 ).TypedValue.GetValueAsLookups()

    If multiIDs.Count > 0 Then
    'if something is selected by user - read the integer ID of the FIRST item selected (I believe that user select only one project!!!)
    Dim prjID '
    Set prjID = multiIDs.Item(1).Item

    'build and ObjID with the selected ID and the OBJECTTYPEID of the Project Object
    Dim ObjID
    Set ObjID = CreateObject("MFilesAPI.ObjID")
    ObjID.SetIDs PROJECT_OBJECTTYPE_ID, prjID '

    'get all properties of the Project selected
    Dim prjProps
    Set prjProps = Vault.ObjectOperations.GetLatestObjectVersionAndProperties(ObjID, true, true)

    'and at the end read the projectnumber property that you are looking for
    prjnum = prjProps.Properties.SearchForProperty ( 1017 ).TypedValue.DisplayValue
    Else
    prjnum = ""
    End if


    I leave as an "exercise" to copy and past this new code in your previous prototype that is correct, and please test the entire code because I write it without possibility to debug!

    BR Lele (codbit)