9
Page | 1 SMALL SCRIPT SPECIFICATION event handler to head file dimensions DEVELOPER: CLIENT:Laminin Solutions Ltd - UK: CREATED ON 41227 LAST UPDATED ON:41227 FUNCTIONAL DESCRIPTION Event handler to automatically save the dimensions of a file to a property for a specific class of documents. CLASSES & PROPERTIES ENTITY TYPE ID DESCRIPTION Object type Document 0 Documents Class 100 Graphic documents Property 1500 File length Property 1501 File width

Event Handler to Head File Dimensions (ID 20534)

Embed Size (px)

Citation preview

Page 1: Event Handler to Head File Dimensions (ID 20534)

P a g e | 1

SMALL SCRIPT SPECIFICATION

event handler to head file dimensions

DEVELOPER:

CLIENT:Laminin Solutions Ltd - UK: CREATED ON 14/11/2012

LAST UPDATED ON:14/11/2012

FUNCTIONAL DESCRIPTION

Event handler to automatically save the dimensions of a file to a property for a specific class of documents.

CLASSES & PROPERTIES

ENTITY TYPE ID DESCRIPTION

Object type Document 0 Documents

Class 100 Graphic documents

Property 1500 File length

Property 1501 File width

FUNCTION CALL

Event handler: BeforeCheckinChanges

LOGIC

On check in

Page 2: Event Handler to Head File Dimensions (ID 20534)

P a g e | 2

Read dimensions of file into variables

Populate properties with dimentions

SAMPLE VB CODE TO READ DIMENSIONS OF A FILE

http://www.freevbcode.com/ShowCode.asp?ID=112

Option Explicit

' Only the first X bytes of the file are read into a byte array.' BUFFERSIZE is X. A larger number will use more memory and' be slower. A smaller number may not be able to decode all' JPEG files. Feel free to play with this number.Private Const BUFFERSIZE As Long = 65535

' image type enumPublic Enum eImageType itUNKNOWN = 0 itGIF = 1 itJPEG = 2 itPNG = 3 itBMP = 4End Enum

' private member variablesPrivate m_Width As LongPrivate m_Height As LongPrivate m_Depth As BytePrivate m_ImageType As eImageType

'' CImageInfo'' Author: David Crowell' [email protected]' http://www.qtm.net/~davidc'' Released to the public domain' use however you wish'' CImageInfo will get the image type ,dimensions, and' color depth from JPG, PNG, BMP, and GIF files.'' version date: June 16, 1999'' http://www.wotsit.org is a good source of' file format information. This code would not have been' possible without the files I found there.'

' read-only properties

Public Property Get Width() As Long Width = m_WidthEnd Property

Public Property Get Height() As Long

Page 3: Event Handler to Head File Dimensions (ID 20534)

P a g e | 3

Height = m_HeightEnd Property

Public Property Get Depth() As Byte Depth = m_DepthEnd Property

Public Property Get ImageType() As eImageType ImageType = m_ImageTypeEnd Property

Public Sub ReadImageInfo(sFileName As String)' This is the sub to call to retrieve information on a file. ' Byte array buffer to store part of the file Dim bBuf(BUFFERSIZE) As Byte ' Open file number Dim iFN As Integer ' Set all properties to default values m_Width = 0 m_Height = 0 m_Depth = 0 m_ImageType = itUNKNOWN ' here we will load the first part of a file into a byte 'array the amount of the file stored here depends on 'the BUFFERSIZE constant iFN = FreeFile Open sFileName For Binary As iFN Get #iFN, 1, bBuf() Close iFN If bBuf(0) = 137 And bBuf(1) = 80 And bBuf(2) = 78 Then ' this is a PNG file m_ImageType = itPNG ' get bit depth Select Case bBuf(25) Case 0 ' greyscale m_Depth = bBuf(24) Case 2 ' RGB encoded m_Depth = bBuf(24) * 3 Case 3 ' Palette based, 8 bpp m_Depth = 8 Case 4 ' greyscale with alpha m_Depth = bBuf(24) * 2 Case 6 ' RGB encoded with alpha m_Depth = bBuf(24) * 4

Page 4: Event Handler to Head File Dimensions (ID 20534)

P a g e | 4

Case Else ' This value is outside of it's normal range, so 'we'll assume ' that this is not a valid file m_ImageType = itUNKNOWN End Select If m_ImageType Then ' if the image is valid then ' get the width m_Width = Mult(bBuf(19), bBuf(18)) ' get the height m_Height = Mult(bBuf(23), bBuf(22)) End If End If If bBuf(0) = 71 And bBuf(1) = 73 And bBuf(2) = 70 Then ' this is a GIF file m_ImageType = itGIF ' get the width m_Width = Mult(bBuf(6), bBuf(7)) ' get the height m_Height = Mult(bBuf(8), bBuf(9)) ' get bit depth m_Depth = (bBuf(10) And 7) + 1 End If If bBuf(0) = 66 And bBuf(1) = 77 Then ' this is a BMP file m_ImageType = itBMP ' get the width m_Width = Mult(bBuf(18), bBuf(19)) ' get the height m_Height = Mult(bBuf(22), bBuf(23)) ' get bit depth m_Depth = bBuf(28) End If

If m_ImageType = itUNKNOWN Then ' if the file is not one of the above type then ' check to see if it is a JPEG file Dim lPos As Long Do ' loop through looking for the byte sequence FF,D8,FF ' which marks the begining of a JPEG file ' lPos will be left at the postion of the start If (bBuf(lPos) = &HFF And bBuf(lPos + 1) = &HD8 _

Page 5: Event Handler to Head File Dimensions (ID 20534)

P a g e | 5

And bBuf(lPos + 2) = &HFF) _ Or (lPos >= BUFFERSIZE - 10) Then Exit Do ' move our pointer up lPos = lPos + 1 ' and continue Loop lPos = lPos + 2 If lPos >= BUFFERSIZE - 10 Then Exit Sub Do' loop through the markers until we find the one 'starting with FF,C0 which is the block containing the 'image information Do ' loop until we find the beginning of the next marker If bBuf(lPos) = &HFF And bBuf(lPos + 1) _ <> &HFF Then Exit Do lPos = lPos + 1 If lPos >= BUFFERSIZE - 10 Then Exit Sub Loop ' move pointer up lPos = lPos + 1 Select Case bBuf(lPos) Case &HC0 To &HC3, &HC5 To &HC7, &HC9 To &HCB, _ &HCD To &HCF ' we found the right block Exit Do End Select ' otherwise keep looking lPos = lPos + Mult(bBuf(lPos + 2), bBuf(lPos + 1)) ' check for end of buffer If lPos >= BUFFERSIZE - 10 Then Exit Sub Loop ' If we've gotten this far it is a JPEG and we are ready ' to grab the information. m_ImageType = itJPEG ' get the height m_Height = Mult(bBuf(lPos + 5), bBuf(lPos + 4)) ' get the width m_Width = Mult(bBuf(lPos + 7), bBuf(lPos + 6)) ' get the color depth m_Depth = bBuf(lPos + 8) * 8 End If

Page 6: Event Handler to Head File Dimensions (ID 20534)

P a g e | 6

End SubPrivate Function Mult(lsb As Byte, msb As Byte) As Long Mult = lsb + (msb * CLng(256))End Function

SAMPLE EVENT HANDLER (CHECKING FOR DUPLICATES)

Option Explicit'' Check on batch creation that a batch with the same sales order with batch nr does not exist'

' Get the properties of the object

Const I_OBJTYPE = 186 ' Sales order Object TypeConst I_PROPDEF = 1257 ' Sales order numberConst I_PROPDEF2 = 1266 ' Batch Reference

Dim PropertyValues : Set PropertyValues = Vault.ObjectPropertyOperations.GetProperties( ObjVer, True )

if( ObjVer.Type = I_OBJTYPE) then

'Err.Raise MFScriptCancel, "Error : " & I_OBJTYPE & " " & OBJVER.ID

' Get current propertyDim szCurrentValue, oCurrentValue, szCurrentValue2szCurrentValue2 =

PropertyValues.SearchForProperty(I_PROPDEF2).TypedValue.DisplayValue

If PropertyValues.IndexOf(I_PROPDEF) = -1 ThenERr.Raise MFScriptCancel, "Please add the Order Reference as a property

and remove the owner order reference as a property and try again"Else

Set oCurrentValue = PropertyValues.SearchForProperty(I_PROPDEF).TypedValue.GetValueAsLookups

End If

' Create seach string szCurrentValue =

PropertyValues.SearchForProperty(I_PROPDEF).TypedValue.DisplayValue & "/" & szCurrentValue2

' Search for objects on the basis of title.Dim oScDim oScs : Set oScs = CreateObject("MFilesAPI.SearchConditions")

' Orders onlySet oSc = CreateObject("MFilesAPI.SearchCondition")oSc.ConditionType = MFConditionTypeEqualoSc.Expression.DataStatusValueType = MFStatusTypeObjectTypeIDoSc.TypedValue.SetValue MFDatatypeLookup, I_OBJTYPEoScs.Add -1, oSc

' Not deletedSet oSc = CreateObject("MFilesAPI.SearchCondition")oSc.ConditionType = MFConditionTypeEqualoSc.Expression.DataStatusValueType = MFStatusTypeDeletedoSc.TypedValue.SetValue MFDatatypeBoolean, False

Page 7: Event Handler to Head File Dimensions (ID 20534)

P a g e | 7

oScs.Add -1, oSc

' Get the ORder ID of the object.Set oSc = CreateObject("MFilesAPI.SearchCondition")oSc.ConditionType = MFConditionTypeEqualoSc.Expression.DataPropertyValuePropertyDef = I_PROPDEFoSc.TypedValue.SetValueToMultiSelectLookup oCurrentValueoScs.Add -1, oSc

' Get the other Document ID of the object.Set oSc = CreateObject("MFilesAPI.SearchCondition")oSc.ConditionType = MFConditionTypeEqualoSc.Expression.DataPropertyValuePropertyDef = I_PROPDEF2oSc.TypedValue.SetValue MFDatatypeText, szCurrentValue2oScs.Add -1, oSc

' Dim currentDocumentID : currentDocumentID = szCurrentValue'oSc.TypedValue.SetValue 1, szCurrentValue ' 1 = MFDatatypeText'oSc.ConditionType = MFConditionTypeStartsWith'oScs.Add -1, oSc

Dim oResults 'As ObjectSearchResultsSet oResults = Vault.ObjectSearchOperations.SearchForObjectsByConditions(oScs,

MFSearchFlagNone, False) 'False = SortResults

If oResults.Count > 0 ThenDim i : Dim oRes 'As MFilesAPI.ObjectVersionFor i=1 To oResults.Count

Set oRes = oResults.Item(i)If oRes.ObjVer.ID<>ObjVer.ID Then

Err.Raise MFScriptCancel, "DUPLICATE - The Sales Order: " & szCurrentValue & " already exists. Try again with different Batch number."

End ifNext

End if

End if

' Err.Raise MFScriptCancel, "Made it through"