//  Export Attributes to CSV File

/*
    Export Selected Attributes to CSV File.
    
    smartDXL.com
*/


DB      dbMain        = null
DBE     dbeAttrList   = null
DBE     dbeExport     = null
DBE     dbeExportPath = null

Module  currModule    = null
Skip    skipAttrs     = null
int     numVisible    = 0  

  
const  int    MAX_VISIBLE = 40    // maximal number of attributes visible in multilist
const  string dummy[]     = {}

string tempDir = getenv("TEMP")

/******************************************************************************
	getAttrs
	
	Fill a skip list with names of visible attributes.
******************************************************************************/
void getAttrs()
{
	AttrDef ad = null
	
	skipAttrs = createString
	
	for ad in currModule do
	{
		if (isVisibleAttribute(ad))
		{
			put(skipAttrs, ad.name, ad)
			numVisible++
		}
	}
	
	// restrict the number of visible entries in the multilist
	if (numVisible > MAX_VISIBLE)
	{
		numVisible = MAX_VISIBLE
	}
}


/******************************************************************************
	fillAttrList
	
	Populates a multilist with the attribute names from the skip list.
	
******************************************************************************/
void fillAttrList()
{
	AttrDef ad = null
	int     i  = 0
	
	for ad in skipAttrs do
	{
		insert(dbeAttrList, i++, ad.name)
	}
}
         

/******************************************************************************
	doList
******************************************************************************/
void doExport(DB db)
{
	int     i           = 0
	string  attr        = ""
	int     numSelected = 0
	Object  o           = null
	Stream  outFile     = null
	AttrDef      ad     = null
	AttrType     at     = null
	AttrBaseType abt    = null
	
	
	// count the number of attributes selected
    numSelected = 0
    for attr in dbeAttrList do numSelected++

    if (numSelected == 0) 
    {
		infoBox("Please select some Attributes!")
		return
    }
	
	outFile = write(get(dbeExportPath))
	
	// write header row
	for attr in dbeAttrList do
	{
		outFile << "\"" attr "\","
	}
	
	outFile << "\n"

	// export attribute values
	for o in currModule do
	{
		for attr in dbeAttrList do
		{
			if (o.attr "" == "")
			{
				outFile << ","
			}
			else if (attr == "Object Heading")
			{
				outFile << "\"" << number(o) " " o.attr "\","
			}
			else
			{
				ad  = find(currModule, attr)
				at  = ad.type
				abt = at.type
				
				if (abt == attrInteger || abt == attrReal)
				{
					outFile << o.attr ","
				}
				else
				{
					outFile << "\"" << o.attr "\","
				}
			}
		}
		
		outFile << "\n"
	}	
	
	close(outFile)
	
	release db
}


/******************************************************************************
	MAIN
******************************************************************************/
currModule = current Module

if (null currModule)
{
	infoBox("This utility can only be run from a formal module.")
	halt
}

getAttrs()

dbMain = create(currModule, "Export Attributes to CSV File")

label(dbMain, "This utility exports selected attributes from the current module\n" //-
              "Output is to a CSV file.")

dbeAttrList = multiList(dbMain, "Attributes to Display:", numVisible, dummy)              
                           
dbeExport     = field(dbMain, "Module", fullName(currModule), 50, true)
dbeExportPath = fileName(dbMain, "Report File", tempDir "\\" name(currModule) " Attributes.csv")

ok(dbMain, "Export", doExport)
	
realize dbMain

fillAttrList()

block(dbMain)

destroy(dbMain)
dbMain = null



sitemap