I'll create a 'hello world' example for you probably next week. It might take me some my time, and this week is just terribly busy.
It will do the ptc_common_name:d=ptc_common_name:mdl thing mentioned in Jose's blogpost. I'll try to do that one without AHK as well as with AHK.
There is just one thing i'd like to know. When you define a part do you give it different common name from the model name? Can you give me an example of how you name your part and assembly files?
Here is the thing. It's prolly not foolproof, but at least something for starters.
First of all, you need to install AHK as there is no standalone compiler that'd be downloadable as an executable anymore. You can get AHK from here: http://www.ahkscript.org/
Once you install AHK the compiler should be located under Start -> AutoHotkey -> Convert .ahk to .exe
Next you need to import the following nested mapkey into Creo Parametric:
!******************* Hello World AHK mapkey example **************************
Then put the mapkey icon on your ribbon menu. This step is kind of important as for this is a Hello World tutorial. I just kind of wouldn't know where to put the Hello World phrase otherwise. The thing is when learning new stuff, one should always start with a Hello World example.
Now save the following code to AutoHotkey-Script.ahk file on your desktop.
;Specify the path to model info file ModelInfoFilePath := A_Temp . "\model_info.lst.1"
;Read the general mapkey header file FileRead, MapkeyHeader, % A_ScriptDir . "\MapkeyHeader.pro"
;Start building up the mapkey contentsOfTheWholeMapkey .= MapkeyHeader
;Check if the model info file file exists and is readable If (!FileExist(ModelInfoFilePath) || ErrorLevel) { FileDelete, % A_Temp . "\model_info.lst.*" ;Display error msg MsgBox, 0x2010, Error, (LTrim Model info file does not exist.
;Read the 3rd line of the model info file FileReadLine, modelInfo3rdLine, % A_Temp . "\model_info.lst.1", 3
;Check if the 3rd line contains the string "PART" If !InStr(modelInfo3rdLine, "PART") { FileDelete, % A_Temp . "\model_info.lst.*" ;Display error msg MsgBox, 0x2010, Error, (LTrim Model is not of a PART type.
;Remove the text before part name partNameCreoParametric := RegExReplace(modelInfo3rdLine, "^.*?PART\s", "")
;Specify the path to the excel file ExcelFilePath := A_ScriptDir . "\stuff.xlsx"
oExcel := ComObjCreate("Excel.Application") ;Create excel object oExcel.Workbooks.Open(ExcelFilePath) ;Open the selected existing file oExcel.Visible := false ;Set to false if you don't need excel to be seen oExcel.DisplayAlerts := false ;This is set to false to suppress prompts and alert messages
;Read through the rows of the excel file Loop, 11 ; Data is only filled up to the line 10 in the excel file { partNameExcel := oExcel.Worksheets(1).Range("B" . A_Index).Value If (partNameExcel = partNameCreoParametric) { commonNameExcel := oExcel.Worksheets(1).Range("C" . A_Index).Value break } }
/** * Explanation: * cellVal := oExcel.Worksheets("Sheet1").Range("A1").Value ; get value from "Sheet1" sheet, "A1" cell */
oExcel.Quit() ;Close the excel window oExcel := "" ;Release the reference
FileDelete, % A_Temp . "\model_info.lst.*"
If (commonNameExcel != "") { ;Read the specific mapkey file FileRead, MapkeyBody, % A_ScriptDir . "\MapkeyFile.pro" ;Remove first line, the mapkey header MapkeyBody := RegExReplace(MapkeyBody, "(^.*?\R)", "") ;Add additional carriage return and new linefeed chars contentsOfTheWholeMapkey .= "`r`n" ;Replace the text COMMON_NAME_TO_REPLACE in the mapkey body with the commonNameExcel value MapkeyBody := StrReplace(MapkeyBody, "COMMON_NAME_TO_REPLACE", commonNameExcel) ;Finish building up the mapkey contentsOfTheWholeMapkey .= MapkeyBody }
/** * Mapkey ">loadmapkey1" performs deletion of the file after this script is done, * but delete it anyway just in case the file already exists. */ FileDelete, % A_Temp . "\mod_mapkey.pro" FileAppend, % contentsOfTheWholeMapkey, % A_Temp . "\mod_mapkey.pro" ;MsgBox, % contentsOfTheWholeMapkey ;for debugging
;Force the script to terminate itself ExitApp
Compile the AutoHotkey-Script.ahk file to AutoHotkey-Script.exe file using the compiler mentioned above. The compiled executable has to be placed on your desktop as the mapkey above hints, the path is:
C:\Users\%USERNAME%\Desktop\AutoHotkey-Script.exe
You could also run it from the ahk file directly if you specified the path in the above mapkey as follows:
C:\Users\%USERNAME%\Desktop\AutoHotkey-Script.ahk
Next, save the following line of code as MapkeyHeader.pro on your desktop, make sure you don't add any newlines after:
mapkey >mapkeyfromahk1 @MAPKEY_LABELMapkey from AutoHotkey script;\
Lastly, save the following code as MapkeyFile.pro also on your desktop
mapkey >commonname1 @MAPKEY_LABELRedefine common name;\
That's all there is to it for the setup. You can run it by clicking the Hello World icon on your ribbon menu.
What does this whole frankenstein beast do?
It reads an excel file called stuff.xlsx located on desktop
It goes through column B of the first sheet and if it finds a value that is the same as the part's name then it copies the text from column C and builds the common name change mapkey.
It then loads and runs that mapkey as part of the nested mapkey above.
Finally, following are the OS script lines used to make the sub-mapkeys
REM following OS scripts are used in the "-nestedmapkey" mapkey
xcopy /s /y model_info.lst.1 %temp%\model_info.lst.1* del model_info.lst.*