




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Sample program: grouping of repetitive functions on User Archives in one function.QUESTION:How can I reduce the C code with scripts for accessing User Archive? ANSWER:If you program scripts for accessing User Archive, then there are large volumes of code for the administration tasks. This code is the same for accessing all User Archives. The example here shows an option for grouping the administration tasks together in one function. The script structure looks like this for standard programming methods: 1. Generating the sorting and filtering conditions 2. Setting up a connection to the User ArchiveuaConnectuaQueryArchiveByNameif error: return 3. Setting the sorting and filtering conditionsuaArchiveSetFilteruaArchiveSetSort 4. Opening the queryuaArchiveOpen 5. Actually working with the queryRead, write, delete, insert and so on. 6. Clearing down the connection to the User ArchiveuaArchiveCloseuaReleaseArchiveuaDisconnect If you program everything with fault check in one function, the function becomes too large and unclear.If you take a close look at the list above, you will be able to see that only points 1 and 5 change from case to case.All the other points remain the same no matter what structure the User Archive has. You can split the above structure into 3 functions. 1. The task-related function takes care of point 1. It generates the filters and sorting conditions. 2. A general function that encapsulates all administration actions. This function applies for ALL User Archives. The function receive a callback function as parameter. This callback function is called either for each entry in the query or just once for the entire archive. 3. A callback function, this function process point 5. This division means that you dont have to worry about the administration actions. You program just one function for query generation and one archive-related function, which processes the archive entries. In the following example we use the function uaUtilityEnumRecords. Function description: BOOL uaUtilityEnumRecords( Const char* pszArchiveName ,const char* lpszFilter,const char* lpszSort,BOOL ( UserFunc )( UAHARCHIVE* phUA, void* pUserData ),void* pUserData ) Parameters: pszArchiveName: Pointer to a zero-terminated string. The string contains the name of the archive as it appears in the User Archive editor. lpszFilter: Pointer to a zero-terminated string. The string contains the filter condition for the archive. The pointer can also be ZERO. lpszSort: Pointer to a zero-terminated string. The string contains the sorting condition for the archive. The pointer can also be ZERO. UserFunc: Name of a callback function. The function is called for each entry if you transfer a filter. The function is called once for the entire archive if you do not transfer a filter. The function must be of the following type:BOOL ( UserFunc )( UAHARCHIVE* phUA, void* pUserData ). pUserData: Pointer to user data. The pointer is forwarded without modification to the callback function. Return value: The function returns the value TRUE if: no faults have occurred during connection setup and clear-down. the callback function always returns TRUE. Otherwise the value FALSE is returned. Function:The uaUtilityEnumRecords function carries out the following steps: 1. Sets up connection to the User Archive. 2. Sets the filter and sorting conditions if they are transferred. 3. Opens the query. 4. If no filter has been set, the function calls the callback function with the handle to the archive.If the filter exists, the callback function is called in a loop for each entry in the query with the handle to an entry. As soon as the CB function returns the value FALSE, the loop is terminated. The function uaUtilityEnumRecords then returns the FALSE. 5. After the callback function has been processed, the connection to the archive is set up. You see the code of the function in the following PDF document:uaUtilityEnumRecords_1.pdf ( 7 KB ) 代码:#include apdefap.hBOOL uaUtilityEnumRecords( const char* pszArchiveName , const char* lpszFilter, const char* lpszSort,BOOL ( UserFunc ) ( UAHARCHIVE* phUA, void* pUserData ),void* pUserData )/*Function create a Query for a User Archive specified by Filter and sort condition. Function calls for eachRecord in Query User function if Filter specified. If not filter spezified function calls user function only onetime.ParameterhArchive: Handle to a User ArchivelpszFilter: Pointer to Zerro Terminated string with Filter for User Archive. If Pointer isnt NULL functionmake new Query from Archive, and calls for EACH record User Function !If Pointer is NULL: Function create conection to User Archive whithout filter.In this Case user function is called only ONE TIME !lpszSort: pointer to zeroterminated string with sort conditionlpFunc: Pointer to User Function.pUserData: Pointer to Parameter Struct for User Function.*/BOOL returnCode = TRUE;const char* funcName = uaUtilityEnumRecords ;UAHCONNECT hConnect ;BOOL returnCode =TRUE;/ Get Connection to User ArchivesreturnCode = uaConnect( &hConnect );if( !returnCode | !hConnect )returnCode = FALSE;printf( %s fault in uaConnect rn, funcName );else/ Get Handle to Actual Archive.UAHARCHIVE hArchive;if ( ! uaQueryArchiveByName( hConnect, pszArchiveName , &hArchive ) )printf( %s: uaQueryArchive Error: %drn, funcName , uaGetLastError() );returnCode = FALSE;elseif( lpszFilter ) returnCode = uaArchiveSetFilter( hArchive, lpszFilter );if( lpszSort ) returnCode = ( returnCode & uaArchiveSetSort( hArchive , lpszSort ) );if( !returnCode )printf(%s: uaArchiveSetFilter rn, funcName );else/ Open Data returned by Queryif( uaArchiveOpen( hArchive ) )/ If Filter specified: Go to First Record in Queryif ( ! lpszFilter ) / if none Filter specified: call user funkction one Time onlyif( !( returnCode = UserFunc( &hArchive , pUserData ) ) )printf(%s error in User Funcion ! rn , funcName) ;else / Filter angegeben ( Filter specified )BOOL isRecord = uaArchiveMoveFirst ( hArchive ) ;/ If Filter specified: For each Record in Query : iterate all Records and Call User Functionwhile( isRecord & lpszFilter )if( !( returnCode = UserFunc( &hArchive , pUserData ) ) )printf(%s error in User Funcion ! rn , funcName) ;isRecord = uaArchiveMoveNext( hArchive ); /. while. / if !Filterif ( !uaArchiveClose ( hArchive )printf(%s error in uaArchiveClose ! rn , funcName) ;returnCode = FALSE ;/ uaArchiveOpen / . SetFilterif( !uaReleaseArchive ( hArchive ) )printf(%s error in uaReleaseArchive! rn , funcName) ;returnCode = FALSE ; / uaQueryArchiveByNameif( !uaDisconnect( hConnect ) )printf(%s error in uaDisconnect ! rn , funcName) ;returnCode = FALSE ; / . if( !returnCode | !hConnect )return returnCode;Interface of the callback functions: phUA: Pointer to a User Archive handle. The handle can refer to the entire archive or just one single entry. pUserData: Pointer to the user data. Return value:In the case of faults, functions should return the value FALSE. Sample project: The sample project demonstrates the application of the function uaUtilityEnumRecords. The sample project was created with WinCC V5.0. You can also use the function with WinCC V 4.02. The sample project contains sample User Archive called JobData. The archive has two columns. One column for the product designation and the other column for the product quantity. In the start image there are four buttons that access the archive. The Insert Job button inserts a new line in the archive.InsertButton_1.pdf ( 3 KB ) Button Insert Job#include apdefap.hvoid OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName)if( uaUtilityEnumRecords( JobData , NULL , NULL , InsertJobCB , NULL ) )printf( Insert O.K. rn );else printf( Insert failed ! rn );Function InsertJobCB#include apdefap.hBOOL InsertJobCB( UAHARCHIVE* phArchive , void* pUserData )BOOL rc = TRUE; / Return codechar* szProductName = GetTagCharWait( ProductName ); / read name of productlong lPartsToProd = ( long ) GetTagWord( BlanksToProduce ); / read number of partsif( ! uaArchiveSetFieldValueString( *phArchive, 1 , szProductName ) | / write productname to archive! uaArchiveSetFieldValueLong( *phArchive, 2 , lPartsToProd ) ) / write number of parts to archiveprintf( InsertJobCB: error on writing to record rn );rc = FALSE ;elseif( !uaArchiveInsert( *phArchive ) ) / insert new line in archiveprintf( InsertJobCB: error on updating archive rn );rc = FALSE ;return rc; The Change Job button sets the quantity for the specified job ID.Change_Job_Button_1.pdf ( 3 KB ) Button Change Job#include apdefap.hvoid OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName)long presetValue = GetTagWord( PresetNumber );if( uaUtilityEnumRecords( JobData, ID0 , NULL , SetPartsNumberCB , &presetValue ) )printf( Preseting Values: O.K. rn );elseprintf( Preseting Values failed rn );Funktion SetPartsNumberCB#include apdefap.hBOOL SetPartsNumberCB( UAHARCHIVE* phArchive , void* pUserData )BOOL rc = TRUE ; / return codeif( ! uaArchiveSetFieldValueLong( *phArchive, 2 , *(long*)pUserData ) ) / write number of parts toarchiveprintf( SetPartsNumberCB: error on writing to Archive rn );rc = FALSE ;elseif( !uaArchiveUpdate( *phArchive ) ) / update archive lineprintf( SetPartsNumberCB: error on updating Archive rn );rc = FALSE ;return rc ; The Get Job with biggest Amount button provides the name for the job with the largest quantity specified.Get_biggestJob_Button_1.pdf ( 3 KB ) Button Get Job with biggest amount#include apdefap.hvoid OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName)char productname100;if ( uaUtilityEnumRecords(JobData, NULL , Amount DESC , GetNameCB , productname ) )SetTagChar( ProductNameBiggestJob , productname );printf( Get Name O.K. rn ) ;elseprintf( Get Name failed rn ) ;Function GetNameCB#include apdefap.hBOOL GetNameCB( UAHARCHIVE* phArchive , void* pUserData )if( !uaArchiveMoveFirst( *phArchive ) ) return FALSE ;return uaArchiveGetFieldValueString( *phArchive, 1 , pUserData, 50 ); The Preset No. of Parts Amount button sets the specified amount for each job.Preset_No_of_PartsButton_1.pdf ( 3 KB ) Button Preset No. of Parts#include apdefap.hvoid OnClick(char* lpszPictureName, char* lpszObjectName, char* lpszPropertyName)long id = GetTagDWord( ID ) ;long parts = GetTagWord( BlanksToProduce );char filter 20 ;sprintf( filter, ID=%d, id );if ( uaUtilityEnumRecords(JobData, filter, NULL , SetPartsNumberCB, &parts ) )printf( Update Job: O.K rn
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 江苏南京建邺区五校联考2026届化学九年级第一学期期中达标检测模拟试题含解析
- 黑龙江省哈尔滨市顺迈2026届九年级化学第一学期期中质量检测模拟试题含解析
- 四川省巴中市2025-2026学年高三上学期9月零诊考试数学试题(含答案)
- 山东省临沂蒙阴县联考2026届九年级化学第一学期期中调研模拟试题含解析
- 2025年新训政府专职消防员职业技能鉴定理论参考试题库(含答案)
- 河南省南阳市内乡县2026届化学九上期中监测模拟试题含解析
- 福建省龙岩市永定区2026届化学九年级第一学期期中经典模拟试题含解析
- 智能制造行业技术工人派遣合同及技能提升协议
- 离婚抚养权变更及子女财产继承协议范本
- 离婚协议书范本:保障离婚后子女教育权益
- GB/T 41972-2022铸铁件铸造缺陷分类及命名
- YY/T 0471.3-2004接触性创面敷料试验方法 第3部分:阻水性
- GB/T 3871.9-2006农业拖拉机试验规程第9部分:牵引功率试验
- PEP小学英语五年级上册第四单元全国优质课赛课一等奖《思维导图在小学英语复习课的应用》精品课件
- 新闻传播中的媒介素养课件
- 小军师面试万能绝杀模板-组织管理
- 超疏水材料课件
- 中医刮痧法诊疗操作评分标准
- 腧穴定位法课件
- 社会体育导论PTPPT课件讲义
- 学校体育学(第三版)ppt全套教学课件
评论
0/150
提交评论