cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

How to create AHK scripts

cyılmaz
8-Gravel

How to create AHK scripts

Hi James62 , we are waiting for you  😃

i want to learn how to create AHK scripts.

1.JPG

6 REPLIES 6

hi,

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?

Thank you 

if my data is;

FAN_KASNAK_CAPI                         =500

MOTOR_KASNAK_CAPI                   =400

E_MESAFEFSI                                  =1200

KAYIS SAYISI                                    =4

common name list;

FAN_KASNAK_KORUYUCU              =   500_400_1200_4_1_R/L

MOTOR_KASNAK_KORUYUCU        =  500_400_1200_4_2_R/L

KAYIS_KASNAK_KORUYUCU           =  500_400_1200_4_3_R/L

KAYIS_KASNAK_ON_KORUYUCU   =  500_400_1200_4_4_R/L

common name list will change when my data change ( in excel ) and i'll send to creo

3.JPG

1.JPG

2.JPG

hi, ‌,

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

ahk1.PNG

Next you need to import the following nested mapkey into Creo Parametric:

!******************* Hello World AHK mapkey example **************************

mapkey -nestedmapkey @MAPKEY_NAMEFrankenstein mapkey example;\

mapkey(continued) @MAPKEY_LABELHello World;\

mapkey(continued) %>exportmdlinf1;%>movemdlinffile1;%>ahkscriptexec1;\

mapkey(continued) %>movemapkeyfile1;%>loadmapkey1;%>mapkeyfromahk1;

mapkey >exportmdlinf1 @MAPKEY_LABELExport ProProgram to file;\

mapkey(continued) ~ Command `ProCmdMmProgram` ;#SHOW DESIGN;\

mapkey(continued) ~ Select `texttool` `MenuBar` 1 `FileMenu`;~ Close `texttool` `MenuBar`;\

mapkey(continued) ~ Activate `texttool` `SaveAsPushButton`;\

mapkey(continued) ~ Update `file_saveas` `Inputname` `model_info.lst`;\

mapkey(continued) ~ Activate `file_saveas` `OK`;~ FocusIn `texttool` `textPH.TextArea`;\

mapkey(continued) ~ Activate `texttool` `CloseButton`;#DONE/RETURN;

mapkey >movemdlinffile1 @MAPKEY_LABELMove the model info file to temp dir;\

mapkey(continued) @SYSTEMxcopy /s /y model_info.lst.1 \%temp\%\\model_info.lst.1*\ndel \

mapkey(continued) model_info.lst.*;

mapkey >ahkscriptexec1 @MAPKEY_LABELExecute the ahk script and wait till done;\

mapkey(continued) @SYSTEMstart /wait C:\\Users\\\%username\%\\Desktop\\AutoHotkey-Script.exe;

mapkey >movemapkeyfile1 @MAPKEY_LABELMove the mapkey config file to work dir;\

mapkey(continued) @SYSTEMxcopy /s /y \%temp\%\\mod_mapkey.pro mod_mapkey.pro*\ndel \

mapkey(continued) \%temp\%\\mod_mapkey.pro;

mapkey >loadmapkey1 @MAPKEY_LABELLoad mapkey into config and delete config;\

mapkey(continued) ~ Select `main_dlg_cur` `appl_casc`;~ Close `main_dlg_cur` `appl_casc`;\

mapkey(continued) ~ Command `ProCmdRibbonOptionsDlg` ;\

mapkey(continued) ~ Select `ribbon_options_dialog` `PageSwitcherPageList` 1 `ConfigLayout`;\

mapkey(continued) ~ Select `ribbon_options_dialog` `ConfigLayout.ImportExportBtn`;\

mapkey(continued) ~ Close `ribbon_options_dialog` `ConfigLayout.ImportExportBtn`;\

mapkey(continued) ~ Activate `ribbon_options_dialog` `ConfigLayout.Open`;\

mapkey(continued) ~ Trail `UI Desktop` `UI Desktop` `DLG_PREVIEW_POST` `file_open`;\

mapkey(continued) ~ Activate `file_open` `Current Dir`;\

mapkey(continued) ~ Select `file_open` `Ph_list.Filelist` 1 `mod_mapkey.pro`;\

mapkey(continued) ~ Trail `UI Desktop` `UI Desktop` `PREVIEW_POPUP_TIMER` \

mapkey(continued) `file_open:Ph_list.Filelist:<NULL>`;\

mapkey(continued) ~ Command `ProFileSelPushOpen@context_dlg_open_cmd` ;\

mapkey(continued) ~ Activate `ribbon_options_dialog` `OkPshBtn`;\

mapkey(continued) ~ FocusIn `UITools Msg Dialog Future` `no`;\

mapkey(continued) ~ Activate `UITools Msg Dialog Future` `no`;\

mapkey(continued) @SYSTEMdel mod_mapkey.pro;

!*****************************************************************************

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.

ahk2.PNG

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"

;Initialize empty vars (propably unnecessary)
commonNameExcel := ""
contentsOfTheWholeMapkey := ""

;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.
       
        The script will be terminated now.
       
    )
    FileDelete, % A_Temp . "\mod_mapkey.pro"
    ;Remove the trailing slash
    contentsOfTheWholeMapkey := RegExReplace(contentsOfTheWholeMapkey, "\\$", "")
    FileAppend, % contentsOfTheWholeMapkey, % A_Temp . "\mod_mapkey.pro"
    ;MsgBox, % contentsOfTheWholeMapkey ;for debugging
    ExitApp
}

;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.
       
        The script will be terminated now.
       
    )
    FileDelete, % A_Temp . "\mod_mapkey.pro"
    ;Remove the trailing slash
    contentsOfTheWholeMapkey := RegExReplace(contentsOfTheWholeMapkey, "\\$", "")
    FileAppend, % contentsOfTheWholeMapkey, % A_Temp . "\mod_mapkey.pro"
    ;MsgBox, % contentsOfTheWholeMapkey ;for debugging
    ExitApp
}

;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;\

mapkey(continued) ~ Command `ProCmdModelRename` ;\

mapkey(continued) ~ Select `rename` `RadioGroup1` 1 `session_only`;\

mapkey(continued) ~ Input `rename` `CommonNameInput` `COMMON_NAME_TO_REPLACE`;\

mapkey(continued) ~ Update `rename` `CommonNameInput` `COMMON_NAME_TO_REPLACE`;\

mapkey(continued) ~ Activate `rename` `OK`;

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

ahk3.PNG

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.

ahk4.PNG

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.*

start /wait C:\Users\%username%\Desktop\AutoHotkey-Script.exe

xcopy /s /y %temp%\mod_mapkey.pro mod_mapkey.pro*
del %temp%\mod_mapkey.pro

thank you so much  for help

1.JPG

What i demonstrated is already a huge step. The one step you need to make first. Once you understand it you can add additional mapkeys to contentsOfTheWholeMapkey variable without much trouble.

First of all, try running just the first submapkey of the nested mapkey.

  1. mapkey -nestedmapkey @MAPKEY_NAMEFrankenstein mapkey example;\ 
  2. mapkey(continued) @MAPKEY_LABELHello World;\ 
  3. mapkey(continued) %>exportmdlinf1;

This should create a file called model_info.lst.1 in your working directory. It's basically an export from Pro/Program. If it does create this file then you can move on with the debugging process.

Note, I'm running Creo Parametric 2.0 M200 on Win 7.

OP, learn basics of java or vb.net

Top Tags