📄 labviewimport.c
字号:
/*------------------------------------------------------------------------------*
* File Name:LabViewImport.c *
* Creation: DVT 7/3/2003 *
* Purpose: OriginC Source C file LabVIEW Import dialog *
* Copyright (c) Originlab Corp. 2003, 2004, 2005, 2006, *
* All Rights Reserved *
* *
* Modification Log: *
*------------------------------------------------------------------------------*/
#include <origin.h>
#include <Dialog.h>
#include "ResizeDialog.h"
#include "GridControl.h"
#include "ODlg.h" // resource IDs, ODlg.dll also use this file for its res ids
#include "HelpID.h"
#include <variant.h> //for GetLVExportedVIs()
#include <VariantTypes.h> //for VT_...
//instead of #include "LV_VI.h" /// DVT 1/27/03 QA70-3725 v7.0498 LABVIEW_IMPORT_DIALOG
#define LV_TREE_NODE_ATTRIBUTE_LABEL "Label"
#define LV_TREE_ROOT_NODE "All_VIs"
#define LV_TREE_ROOT_NODE_LABEL "All VIs"
#define DISPLAY_VARIANT_TRUE "true"
#define DISPLAY_VARIANT_FALSE "false"
#define LT_SHOW_WND "win -a %H"
#define LV_WKS_NAME "LabView"
#define LV_FMT_STR_ARRAY_VEC_LONG "Array of %d vectors, %d long"
#define LV_STR_ARRAY "Array"
#define LV_STR_VECTOR "Vector"
#define LV_STR_NOT_HANDLED "<Not handled>"
// debug macros,
#define LV_DEBUG_INT(_1_, _2_) ;//out_int(_1_, _2_)
#define LV_DEBUG_STR(_arg_) ;//out_str(_arg_)
#define LV_DEBUG_printf(_1_, _2_, _3_) ;//printf(_1_, _2_, _3_)
//end of #include "LV_VI.h"
#define DBG_TREE_OUT(_tr, _lpcsMsg) //tree_dump(_tr, _lpcsMsg, 4);
//////////////// TREE LV TREE LV TREE ////////////////
//////////////// TREE LV TREE LV TREE ////////////////
////////////////////////////////////////////////////////////////////////////////////
// Constants
#define LV_HEADER_START "<VI syntaxVersion="
#define LV_HEADER_START_LENGTH 18
#define LV_CONTROL_START "<CONTROL ID="
#define LV_CONTROL_START_LENGTH 12
#define LV_CONTROL_START_END "\">"
#define LV_CONTROL_START_END_LENGTH 2
#define LV_CONTROL_TYPE "type=\""
#define LV_CONTROL_TYPE_LENGTH 6
#define LV_CONTROL_NAME " name=\""
#define LV_CONTROL_NAME_LENGTH 7
#define LV_CONTROL_END "</CONTROL>"
#define LV_CONTROL_END_LENGTH 10
#define LV_CONTENT "<CONTENT>"
#define LV_CONTENT_LENGTH 8
#define LV_CONTENT_END "</CONTENT>"
#define LV_CONTENT_END_LENGTH 9
#define LV_STR_CONTROL_VAL_UNKNOWN "<?>"
#define LV_STR_EOL "\r"
#define LV_STR_TAB "\t"
//static string s_strNodeName = "e";
#define LV_STR_NODE_NAME "e"
#define LV_TREE_FMT_STR "Tree"
#define LV_LIST_FMT_STR "Name|Type|ID|Value";
#define LV_FILE_OPEN_STR_VI "[LabView (*.vi)] *.vi"
#define LV_COM_OBJ_NAME "LabVIEW.Application"
// Macros
#define RETURN_IF_NOT_FOUND(_n,_b) if(-1==(_n))return _b
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
//////////////// end TREE LV TREE LV TREE ////////////////
//////////////// end TREE LV TREE LV TREE ////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
class VIListControl : public GridListControl
{
bool m_bImport;
public:
bool FindAndGetCell( string &strName, string &strValue )
{
if ( strName.IsEmpty() )
return false;
int nRow(0);
if ( -1 != ( nRow = m_flx.FindRow(strName, m_flx.FixedRows, 0) ) )
{
strValue = m_flx.Cell( flexcpText, nRow, m_flx.Cols - 1 );
return true;
}
return false;
}
void OnBeforeEditList(Control ctrl, int nRow, int nCol, BOOL* pCancel)
{
//for now, enable only the last column editing.
//Should actualy check if even that is editable (not <?>, array, claster and such)
if ( nCol != m_flx.Cols -1 )
*pCancel = true;
}
bool IsImportEnabled()
{
return m_bImport;
}
Clear()
{
m_flx.Rows = 1; //delete all but headers
}
void OnSelChange(Control ctrl)
{
m_bImport = false;
string strVal;
if ( GetSelectedCell( strVal, m_flx.Cols - 1 ) )
m_bImport = -1 != strVal.Find( LV_STR_ARRAY ) ||
-1 != strVal.Find( LV_STR_VECTOR );
return;
}
bool Setup(uint nCols = 4)
{
Clear();
m_flx.FormatString = LV_LIST_FMT_STR
m_flx.Cols = nCols;
m_flx.ExtendLastCol = true;
m_flx.FixedCols = 0; //hide the Row heading
m_flx.SelectionMode = flexSelectionByRow; //Forces selections to span entire rows.
m_flx.AllowUserResizing = flexResizeColumns; //allow columns resizing
m_flx.ExplorerBar = flexExSort; //sort columns by clicking on their headings
m_flx.AutoSizeMode = flexAutoSizeColWidth; //Adjust column widths to accommodate the widest entry in each column
// prevent it from entering edit mode by trapping the BeforeEdit event and setting the Cancel parameter to True
m_flx.Editable = flexEDKbdMouse;//flexEDNone; /// DVT 1/27/03 QA70-3725 v7.0498 LABVIEW_IMPORT_DIALOG
m_flx.AllowSelection = false; //prevent users from extending the selection
return true;
}
bool Update( vector<int> &vIDs, vector<string> &vTypes, vector<string> &vNames, vector<string> &vValues )
{
// suspend repainting to increase speed
//SetRedraw(FALSE);
Clear(); //delete all but headers
// populate the control: Name|Type|ID|Value
//from vsFlexGrid8.0 FAQ:
//The fastest way to add data is using the TextMatrix property, and the slowest is using the AddItem method.
//If the data is already loaded in an array of Variants, then the BindToArray method is even faster.
//(BindToArray does not actually load the data, it just tells the control where the data is).
for ( int i = 0; i < vIDs.GetSize(); i++ )
{
string strTMP(vIDs[i]);
string strRow = vNames[i] + LV_STR_TAB +
vTypes[i] + LV_STR_TAB +
strTMP + LV_STR_TAB +
vValues[i];
m_flx.AddItem( strRow );
}
// expand outline, resize to fit, collapse outline
m_flx.Outline(-1);
m_flx.AutoSize(0, 3);
m_flx.Outline(1);
// repainting is back on
//SetRedraw(TRUE);
return true;
}
//////////////////////////////////////////////////////////////////////////////////////
//DVT new for dlgclass:
bool GetSelectedCell( string &asName, int nCol = 0 )
{
int nR = m_flx.RowSel;
if ( nR < 0 )
return false;
asName = m_flx.Cell( flexcpText, nR, nCol );
return true;
}
};
class VITreeControl : public GridControl
{
//////////////////////////////////////////////////////////////////////////////////////
bool AddTreeNodes(TreeNode& tNode, int lvl)
{
if(tNode == NULL)
return false;
int nRow;
bool bIsBranch = true;
int nCnt = tNode.GetNodeCount();
string strLabel;
/// DVT 1/27/03 QA70-3725 v7.0498 LABVIEW_IMPORT_DIALOG
if(!tNode.GetAttribute(LV_TREE_NODE_ATTRIBUTE_LABEL, strLabel))
strLabel = tNode.tagName;
/// end LABVIEW_IMPORT_DIALOG
LV_DEBUG_INT(strLabel, lvl)
// this function should save some object in the cell and return the object for future
AddRow(nRow, strLabel, bIsBranch);
m_flx.RowOutlineLevel(nRow) = lvl;
// dump child nodes
TreeNode cNode = tNode.FirstNode;
if(cNode == NULL)
return true;
while(cNode)
{
if(!AddTreeNodes(cNode, lvl + 1))
break;
cNode = cNode.NextNode;
}
return true;
}
DWORD AddRow(long& nRow, string strItem, bool bIsBranch) // = false
{
//strItem += "\t"; // make 2nd col empty text, will set its value later
m_flx.AddItem(strItem);
nRow = m_flx.Rows - 1L;
m_flx.IsSubtotal(nRow) = bIsBranch;
return 0;
}
bool AddTreeNodes( Tree& tNode, int lvl )
{
if(tNode == NULL)
return false;
int nRow(0);
bool bIsBranch(true);
int nCnt( tNode.GetNodeCount() );
string strLabel;
/// DVT 1/27/03 QA70-3725 v7.0498 LABVIEW_IMPORT_DIALOG
if(!tNode.GetAttribute(LV_TREE_NODE_ATTRIBUTE_LABEL, strLabel))
strLabel = tNode.tagName;
/// end LABVIEW_IMPORT_DIALOG
LV_DEBUG_INT(strLabel, lvl)
// this function should save some object in the cell and return the object for future
AddRow(nRow, strLabel, bIsBranch);
m_flx.RowOutlineLevel(nRow) = lvl;
// dump child nodes
TreeNode cNode = tNode.FirstNode;
if(cNode == NULL)
return true;
while(cNode)
{
if(!AddTreeNodes(cNode, lvl + 1))
break;
cNode = cNode.NextNode;
}
return true;
}
Clear()
{
m_flx.Rows = 1; //delete all but headers
}
public:
int GetSelectedRow()
{
int nRowsSelected = m_flx.SelectedRows;
if ( 0 >= nRowsSelected )
return -1;
ASSERT ( 1 == nRowsSelected );
return m_flx.SelectedRow(0);
}
void OnSelChange(Control ctrl)
{
return;
}
bool Setup( uint nCols = 1)
{
Clear();
m_flx.Cols = nCols;
m_flx.FixedCols = 0;
m_flx.ExtendLastCol = true;//false;//true;
m_flx.FormatString = LV_TREE_FMT_STR;
m_flx.ColAlignment(-1) = flexAlignLeftTop;
m_flx.SelectionMode = flexSelectionListBox; //select non-continuous rows
// outline
m_flx.OutlineCol = 0;
m_flx.OutlineBar = flexOutlineBarSimpleLeaf;
m_flx.MergeCells = flexMergeSpill; // allow categories to spill into property column
m_flx.AllowSelection = false; //prevent users from extending the selection
// other
m_flx.AllowUserResizing = flexResizeColumns;
return true;
}
bool Load(Tree& tr)
{
m_flx.Redraw = flexRDNone;
int nRowHeight = m_flx.RowHeight(0);
/// DVT 1/27/03 QA70-3725 v7.0498 LABVIEW_IMPORT_DIALOG
TreeNode tNode = tr.FirstNode;
if(tNode == NULL) //skip OriginStorage node
return false;
AddTreeNodes(tNode, 0);
/// end LABVIEW_IMPORT_DIALOG
//flx.AutoSize(0L, 1L, 0L, 200L);
m_flx.Redraw = flexRDBuffered;
return true;
}
public:
//////////////////////////////////////////////////////////////////////////////////////
//DVT new for dlgclass:
bool GetSelectedCell( string &strVISel )
{
int nRowsSelected = m_flx.SelectedRows;
if ( 0 >= nRowsSelected )
return false;
ASSERT ( 1 == nRowsSelected );
long nR = m_flx.SelectedRow(0);
strVISel = m_flx.Cell( flexcpText, nR, 0 );
return true;
}
};
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////// TREE LV TREE LV TREE ////////////////
//////////////// TREE LV TREE LV TREE ////////////////
class LabVIEW
{
//private:
Tree m_trVI; // tree from caller
Object m_lvapp; // LabVIEW reference
bool m_bNoLabViewTest; //true - LabVIEW connected
public:
// Default Constructor
LabVIEW()
{
m_bNoLabViewTest = false;
m_lvapp = NULL;
}
// Copy Constructor
//LabVIEW( LabVIEW const &lv )
//{
// m_bNoLabViewTest = lv.m_bNoLabViewTest;
// m_lvapp = lv.m_lvapp;
// m_trVI = lv.m_trVI;
//}
bool LVDisconnect()
{
//leak???
m_bNoLabViewTest = false;
return true;
}
//discover all VI's in memory (the name comes from LabView property ExportedVIs)
bool GetLVExportedVIs( vector<string> &vsALLVIs )
{
if(!CheckLabView())
return false;
_VARIANT varVIs;
//varVIs = s_lvapp.AllVIsInMemory; //Access denied :) reconfigure LV server???
try
{
varVIs = m_lvapp.ExportedVIs;
}
catch(int nErr)
{
}
if( 0 >= varVIs.GetSize() )
{
LV_DEBUG_STR("No ExportedVIs\n");
return false;
}
vsALLVIs = varVIs;
return true;
}
/////////////////////////////////////////
//exported for OnRunVI
bool GetVIObjectByName( const string &strVI, Object &viOne )
{
if ( !CheckLabView() )
return false;
//it won't be able to query sub-vi if main vi is closed,
//consequently, following line will fail GetVIReference
try
{
viOne = m_lvapp.GetVIReference( strVI );
}
catch(int nErr)
{
}
if ( !viOne )
{
LV_DEBUG_STR( strVI + "CANNOT get reference!" );
return false;
}
return true;
}
/////////////////////////////////////////
bool CheckLabView( bool bTry = true )
{
if( !bTry )
{
return m_bNoLabViewTest;
}
if( !m_bNoLabViewTest )
{
try
{
m_lvapp = CreateObject( LV_COM_OBJ_NAME );
}
catch(int nErr)
{
}
if( m_lvapp == NULL )
{
LV_DEBUG_STR("Fail to instantiate Labview Application\n");
return false;
}
m_bNoLabViewTest = true;
}
return true;
}
bool IsTopmostVI(string &strVI)
{
TreeNode tNode( m_trVI.FirstNode ); //All_VIs
int nCnt(0);
string strVal( tNode.tagName );
if ( (0 == (nCnt = tNode.GetNodeCount()) ) || (0 != strVal.Compare(LV_TREE_ROOT_NODE) ) )
return false;
tNode = tNode.FirstNode;
while ( tNode )
{
tNode.GetAttribute(LV_TREE_NODE_ATTRIBUTE_LABEL, strVal);
if ( 0 == strVI.Compare( strVal ))
break;
tNode = tNode.NextNode;
}
return tNode ? true : false;
}
bool ExportVIControls( string &strVI, string &strExportPathName )
{
Object viOne;
if ( !GetVIObjectByName( strVI, viOne ) )
return false;
//create LabView's tagged file containing list of controls for given VI
if( !GetTempFileName( strExportPathName ) )
return false;
try
{
viOne.ExportVIStrings( strExportPathName );
}
catch(int nErr)
{
return false;
}
if( strExportPathName.IsFile() )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -