示例#1
0
文件: var_comp.c 项目: koder77/NanoVM
U1 getvarstr_comp (U1 *argstr, S2 pos_start)
{
    S2 pos, pos_end, endvn, dimstart, dimend;
    S4 varind, dims;
    U1 pstr[MAXLINELEN + 1], comm[2], dimensstr[MAXLINELEN + 1], private_var = NORMAL_VAR;
    U1 ok = FALSE;

    U1 varname[MAXLINELEN + 1];
    U1 buf[MAXLINELEN + 1];

    comm[0] = AROPEN_SB;
    comm[1] = BINUL;

     /* bugfix */
    pos_start = 0;
    pos_end = strlen (argstr) - 1;

    endvn = searchstr (argstr, comm, pos_start, pos_end, TRUE);
    if (endvn != -1)
    {
        if (argstr[0] != '_')
        {
            partstr (argstr, varname, pos_start, endvn - 1);
            strcat (varname, AT_SB);
            strcat (varname, function[function_ind].name);
            partstr (argstr, buf, endvn, pos_end);
            strcat (varname, buf);
        }
        else
        {
            partstr (argstr, varname, pos_start, pos_end);
        }
    }
    else
    {
        if (argstr[0] != '_')
        {
            strcpy (varname, argstr);
            strcat (varname, AT_SB);
            strcat (varname, function[function_ind].name);
        }
        else
        {
            /* global variable starting with "_": exp: _foobar */
            strcpy (varname, argstr);
        }
    }

    t_var.dims = NODIMS;
    pos_end = strlen (varname) - 1;

    if (varname[0] == PRIVATE_VAR_SB)
    {
        /* use pvarlist for dimens */

        private_var = PRIVATE_VAR;

        #if DEBUG
            printf ("getvarstr: PRIVATE_VAR SET\n");
        #endif
    }

    partstr (varname, pstr, pos_start, pos_end);

    #if DEBUG
        printf ("getvarstr: argstr: '%s'\n", varname);
    #endif

    /* bugfix */
    pos_start = 0;
    pos_end = strlen (pstr) - 1;

    endvn = searchstr (pstr, comm, pos_start, pos_end, TRUE);
    if (endvn != -1)
    {
        /* area variable, exp. a[5][x] */

        dimstart = endvn;
        endvn--;
        dims = NODIMS;

        while (! ok)
        {
            comm[0] = ARCLOSE_SB;
            comm[1] = BINUL;

            dimend = searchstr (pstr, comm, dimstart + 1, pos_end, TRUE);
            if (dimend != -1)
            {
                if (dims < MAXDIMENS - 1)
                {
                    dims++;
                }
                else
                {
                    printerr (OVERFLOW_IND, plist_ind, ST_PRE, t_var.varname);
                    return (FALSE);
                }

                /*  getstr (U1 *str, U1 *retstr, S2 maxlen, S2 start, S2 end) */

                if (private_var == NORMAL_VAR)
                {
                    getstr (pstr, dimensstr, MAXLINELEN, dimstart + 1, dimend - 1);
                    if (checkdigit (dimensstr) == TRUE)
                    {
                        if (! make_val (INT_VAR, varlist, NORMAL_VAR))
                        {
                            printerr (MEMORY, plist_ind, ST_PRE, t_var.varname);
                            return (FALSE);
                        }
                        t_var.dimens[dims] = t_var.varlist_ind;
                    }
                    else
                    {
                        /* variable, */
                        /* check if already defined */

                        varind = getvarind_comp (dimensstr);
                        if (varind != -1)
                        {
                            if (varlist[varind].type != STRING_VAR && varlist[varind].type != BYTE_VAR)
                            {
                                t_var.dimens[dims] = varind;
                            }
                            else
                            {
                                /* error: its a string or byte var */

                                printerr (WRONG_VARTYPE, plist_ind, ST_PRE, dimensstr);
                                return (FALSE);
                            }
                        }
                        else
                        {
                            printerr (NOTDEF_VAR, plist_ind, ST_PRE, dimensstr);
                            return (FALSE);
                        }
                    }
                }
                else
                {
                    /* private variable */

                    #if DEBUG
                        printf ("getvarstr: PRIVATE VAR!");
                    #endif

                    getstr (pstr, dimensstr, MAXLINELEN, dimstart + 1, dimend - 1);
                    if (checkdigit (dimensstr) == TRUE)
                    {
                        if (! make_val (INT_VAR, pvarlist_obj, PRIVATE_VAR))
                        {
                            printerr (MEMORY, plist_ind, ST_PRE, t_var.varname);
                            return (FALSE);
                        }
                        t_var.dimens[dims] = t_var.varlist_ind;

                        #if DEBUG
                            printf ("getvarstr: PRIVATE VAR: %li", t_var.varlist_ind);
                        #endif
                    }
                    else
                    {
                        /* variable, */
                        /* check if already defined */

                        varind = getvarind_comp (dimensstr);
                        if (varind != -1)
                        {
                            if (pvarlist_obj[varind].type != STRING_VAR && pvarlist_obj[varind].type != BYTE_VAR)
                            {
                                t_var.dimens[dims] = varind;
                            }
                            else
                            {
                                /* error: its a string or byte var */

                                printerr (WRONG_VARTYPE, plist_ind, ST_PRE, dimensstr);
                                return (FALSE);
                            }
                        }
                        else
                        {
                            printerr (NOTDEF_VAR, plist_ind, ST_PRE, dimensstr);
                            return (FALSE);
                        }
                    }
                }
            }
            else
            {
                /* no ] */

                printerr (BRNOT_OK, plist_ind, ST_PRE, "");
                return (FALSE);
            }

            comm[0] = AROPEN_SB;
            comm[1] = BINUL;

            if (dimend < pos_end)
            {
                pos = searchstr (pstr, comm, dimend + 1, pos_end, TRUE);
                if (pos != -1)
                {
                    dimstart = pos;
                }
                else
                {
                    ok = TRUE;
                }
            }
            else
            {
                ok = TRUE;
            }
        }
        getstr (pstr, t_var.varname, MAXVARNAME, pos_start, endvn);
        t_var.dims = dims;

        #if DEBUG
            printf ("getvarstr: dims = %li\n", dims);
        #endif
    }
    else
    {
        getstr (pstr, t_var.varname, MAXVARNAME, pos_start, pos_end);
    }
    return (TRUE);
}
int main( int argc, char *argv[] )
{
	const std::string ASSEMBLE_PTC_VERSION = ISIS_PRODUCT_VERSION_WITH_v_AND_DOTS;
	::boost::filesystem::path original_directory = ::boost::filesystem::current_path();

	int ExitCode = 0;

	std::string			creoStartCommand; 
	std::string			proeIsisExtensionsDir;

	std::string			templateFile_PathAndFileName;
	std::stringstream	exceptionErrorStringStream;
	bool				promptBeforeExiting;

	bool Pro_E_Running = false;

	isis::ProgramInputArguments  programInputArguments;
	programInputArguments.logFileName = "DefaultLogFile.log";
	::boost::filesystem::path    workingDir;

	try
	{
		cout << "ExtractACM-XMLfromCreoModels "<< ASSEMBLE_PTC_VERSION;

// STEP 1: Parse Input Arguments
		isis::ParseInputArguments(argc, argv, programInputArguments);

		promptBeforeExiting = programInputArguments.promptBeforeExiting;

// STEP 2: Configure the Creo start command string
		// Must get the complete path to the working directory.  This is necessary because when
		// isis_ProDirectoryChange is called to change to a STEP directory, workingDir must be fully
		// defined so that isis_ProDirectoryChange can be called to return to the original directory.
		workingDir = isis::SetupWorkingDirectory( programInputArguments.workingDirectory );

		if ( workingDir.generic_string().size() >= PRO_PATH_SIZE )  // PRO_PATH_SIZE   260
		{
			std::stringstream errorString;
			errorString << "WORKING_DIR string too long.  Maximum allowed number of characters: "  << PRO_PATH_SIZE - 1 << " WORKING_DIR string: " << workingDir;
					throw isis::application_exception(errorString);
		}
		::boost::filesystem::current_path(workingDir);

		// configure start string
		bool graphicsModeOn = programInputArguments.graphicsModeOn?true:false;
		bool creoAcceptInputFromThisProgramAndCreoUI = programInputArguments.synchronizeWithCyPhy?true:false;

		isis::SetCreoEnvirVariable_RetrieveSystemSettings(	graphicsModeOn,
															creoAcceptInputFromThisProgramAndCreoUI,
															creoStartCommand,
															proeIsisExtensionsDir,
															templateFile_PathAndFileName );

// STEP 3: Start Creo in async mode
			char tempBuffer[1024];

			strcpy(tempBuffer, creoStartCommand.c_str() );

			cout << endl << "Starting Creo-Parametric takes about 10 seconds, be patient..." << endl ;

			ProError err = isis::isis_ProEngineerStart(tempBuffer,"");

			Pro_E_Running = true;

			// callback in case Creo ends with an error
			ProTermFuncSet(ProTermAction);

// STEP 4: Open the file
			ProMdl mdl;
			isis::MultiFormatString partstr(getpartname(programInputArguments.inputCADFileName));
			err = ProMdlRetrieve((wchar_t*)(const wchar_t*)partstr, fileISasm(programInputArguments.inputCADFileName)?PRO_MDL_ASSEMBLY:PRO_MDL_PART, &mdl);

// STEP 5: Request creo give us an output XML for use in CyPhy
			if(err)
			{
				std::stringstream errorString;
				errorString << "Error starting Creo parametric - Unable to convert file.  Check license server reachability or input file validity.";
				throw isis::application_exception(errorString);
			}

			std::string xmlbuffer = isis::CreoModelToCyphyXML(mdl);

			if(xmlbuffer=="")
			{
				std::stringstream errorString;
				errorString << "CreotoCyPhy call resulted in an empty string. Unable to convert file";
				throw isis::application_exception(errorString);
			}

			// the above call returns a text buffer.  Dump that buffer to a file
			ofstream xmlfile;
			std::string xmlfilename;

			if(programInputArguments.outputXmlFileName == "")
			{
				xmlfilename = programInputArguments.inputCADFileName+".xml";
//				cout <<  endl << "No output file specified, creating new XML file: " << xmlfilename << std::endl ;
			}
			else
			{
				xmlfilename = programInputArguments.outputXmlFileName;
			}
			cout << "Writing to XML file: " << xmlfilename << std::endl << std::endl;
			xmlfile.open (xmlfilename);
			xmlfile << xmlbuffer;
			xmlfile.close();

//// JGG W.I.P. - Create list of part files fould in the assembly
//			// determine if we have an assembly file
//			// ASSUMING that by this point any invalid file type would have barfed by now
//			//  so the file is either .prt or .asm at this point
//			if( fileISasm(programInputArguments.inputCADFileName) )
//			{
//				isis::CreoModelAssemblyAttributes local_out_AssemblyHierarchy;
//				const ProSolid	local_p_solid_handle = (ProSolid)mdl;
//				isis::RetrieveAssemblyHierarchyInformation ( local_p_solid_handle,
//														true,
//														local_out_AssemblyHierarchy  );
//							// Temporary Check
//				cout << "Parsing assembly file..." << std::endl;
//				for each ( isis::CreoModelAssemblyAttributes j in local_out_AssemblyHierarchy.children )
//				{
//					cout << "Found assemply part:  " << j.modelname << std::endl;
//				}
//			}
	} // END Try


    catch ( isis::application_exception& ex )
	{
		exceptionErrorStringStream  << "ERROR: " << ex.what();
		ExitCode = -1;
	}
	catch ( std::exception& ex )
	{
		exceptionErrorStringStream << "ERROR: " << ex.what();
		ExitCode = -2;
	}
	catch ( ... )
	{
		exceptionErrorStringStream << "ERROR: std::exception: Caught exception (...).  Please report the error to the help desk.";
		ExitCode = -3;
	}

	if ( ExitCode != 0 )
	{
		// Write to _FAILED.txt
		std::string failedTxtFileName = "_FAILED.txt";
		bool addLineFeed = false;
		if ( isis::FileExists( failedTxtFileName.c_str() )) addLineFeed = true;

		ofstream failedTxtFileStream;
		failedTxtFileStream.open (failedTxtFileName, ios::app );
		if ( failedTxtFileStream.is_open() )
		{
			if ( addLineFeed ) failedTxtFileStream << std::endl;
			failedTxtFileStream <<  isis::GetDayMonthTimeYear() << ", ExtractACM-XMLfromCreoModels.exe error code: " << ExitCode ;
			failedTxtFileStream << exceptionErrorStringStream << std::endl;
			switch(ExitCode)
			{
			case -1:
				failedTxtFileStream << "This error means a problem running Creo Parametric. Make sure the license server is reachable. Make sure the input file is valid." << std::endl;
				break;
			case -2:
				failedTxtFileStream << "This error code means a problem with this executable.  Double check the input parameters." << std::endl;
				break;
			case -3:
			default:
				failedTxtFileStream << "This error code means an 'other' problem occured.  Check environment and environment variables." << std::endl;
				break;
			}

			failedTxtFileStream.close();
		}

		std::cerr << std::endl << std::endl << exceptionErrorStringStream.str() << std::endl << std::endl;
	}

		/////// Stop Pro/E /////////
		if (Pro_E_Running) isis::isis_ProEngineerEnd();

		if ( promptBeforeExiting )
		{
			printf("\nType Enter to exit.");
			getc(stdin);
		}
    ::boost::filesystem::current_path(original_directory);
	exit(ExitCode);
}