Hi All,
I've looked around and done some research on this and it seems possible just I can't figure out how to do it.
I'm looking to send keystrokes programmically using iLogic as if the keys were actually being pressed. I just want to send a simple set of keys like "QQ".
Does anybody know how I would do this through iLogic?
Even if it is an improper method of programming, is there anyone who can tell me how to do this
Thanks
Mitch
Please create one part document first. When copying the code lines below in VBA Editor dialog and running macro, it will work fine, and this simulates to press the "E" key to create extrusion.
Thanks,
River
Option Explicit
Declare Function PostMessage& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const WM_KEYDOWN As Long = &H100
Private Const WM_KEYUP = &H101
Sub Main()
Dim FT_hWnd As Long
FT_hWnd = FindWindow(vbNullString, ThisApplication.Caption)
Call PostMessage(FT_hWnd, WM_KEYDOWN, Asc("E"), 0&)
Call PostMessage(FT_hWnd, WM_KEYUP, Asc("E"), 0&)
End Sub
Wow, thank you very much!
I've been researching and researching on how to do this and have never come close to anything that looks like this.
Thanks again
Mitch
Hi River,
The code worked great when I manually go in and run the macro. In the end, I would like to activate it and run it through an iLogic Rule so I can control when to use it.
I assumed it was as simple as using the snippet
Format:HTML Format Version:1.0 StartHTML: 165 EndHTML: 1121 StartFragment: 314 EndFragment: 1089 StartSelection: 314 EndSelection: 314
SyntaxEditor Code Snippet
InventorVb.RunMacro("DocumentProject", "Module1", "Main")
But it doesn't seem to be working
Thanks
Mitch
Mitch,
You can use Windows (Win32) code directly in an iLogic rule, instead of running a VBA macro. See the attached rule. I think if you added the call to SetFocus in the VBA code, the VBA macro would also work from an iLogic rule.
However, this rule won't work if it is triggered to run by a parameter change that is made in the Parameters dialog.
If you want to send a key to start an Inventor command, you can do it more easily with the Inventor API call ControlDefinition.Execute or ControlDefinition.Execute2.
Thanks Mike!
I attempted to post the previous code directly into iLogic but it did not work. The only issue with the code you've provided is that it can only use one character. A little fiddling around with it let me find out that if I use the "PostMessage" line twice, it will use two characters.
I'm new to programming and unsure where I would use the ControlDefinition.Execute command. But the code you provided does work! Thank You!
Unfortunately I have but one more question, I thought I knew how to do this but apparently I do not.
Is it possible to access the "Customize" dialog box through code and change the shortcut keys? I would like to do this as a check and to insure that the correct keys I input to the code are indeed what the user has set. Or, save the string they have, and input the characters I desire, then re-input the previous shortcut that the user originally had.
Just for clarity, this is (as far as I know how to access it) Tools Tab > Customize > Keyboard Tab >... And then the command I'm trying to access, for helpfulness, lets say I'm trying to change the Command Name: "Constraint..." from Keys: "C" to Keys: "PP"
Thanks Again
Mitch
Mitch,
Instead of working with the particular command shortcut keys, it's better to start a command with code like this:
Dim oControlDef As ControlDefinition = ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyInsertConstraintCmd")
oControlDef.Execute()
That will start the Constraint command.
You can get the shortcut keys for a command: it's available as the DefaultShortcut or OverrideShortcut property on the ControlDefinition object. But if you have a ControlDefinition object (as in the code above), you might as well just Execute it instead of worrying about the keys.
You need to know the internal name of the command. I attached a list of available command names.
Hi Mike,
The document you provided will definitly help for future coding as I'm learning how to do so. Thank you very much for providing me with this.
Unfortunately, where my issue lies is that I am working with an Add-In from an outside source. I will try to see if I can find the "internal name" given for this, but that is the reason why I am trying to use keys to open the dialog box. I have done quite a bit of research and found that I need to reference the .dll (which I found) as well as use the dialog box file (which I did not find).
So my next thought is to use the shortcut keys to open the dialog box as you can customize this command
Thanks
Mitch
Here is a rule that will list the internal command name for all add-in commands. It will save the list to C:\InventorCommands.txt. You should be able to find your command in there.
Wow, you just did 2 weeks worth of researching and digging in 5 seconds with those codes...
Thank you very much Mike! Five steps closer to model and drawing automation!
Thanks Again
Mitch
Annnnnnnd there's the road block.....
Using the methods you told me I was able to do exactly wat I needed and open the dialog box. This will work great for my simpler models.
Unfortunately, the dialog box pops up for the active document on screen only it seems. So with my assembly which uses various iLogic components and pushes dimensions given by a user down to each component, if I run this rule it will keep open the dialog box and supplying the information only for the assembly itself, while each component needs its seperate properties.
MY QUESTION IS: Is there a way to open a component using iLogic from an assembly?
I attempted a few different things.
- I created a text parameter inside the component and used "ThisDoc.PathAndFile(False)" to get it's information. Then went to the Assembly and grabbed this parameter, attempting to launch it using "ThisDoc.Launch("path/file.ext")" which I now realize is most likely for non-inventor documents
I tried this and a few other things just to see if I could figure it out by myself, but unfortunately I could not.
Thanks again for all your help
Mitch
Sorry, I've figured it out if anybody is interested and needs this later on. My users can change the file names of the documents and the links still stay intact through the "Copy Design" tool in The Vault
Which is why I need to get the filename directly from the component...
Format:HTML Format Version:1.0 StartHTML: 165 EndHTML: 1728 StartFragment: 314 EndFragment: 1696 StartSelection: 314 EndSelection: 314
SyntaxEditor Code Snippet
fname=Parameter("Outlet_Flange:1", "DocumentName")&".ipt"
doc=ThisApplication.Documents.Open(fname)
Cheers
Mitch
You can do it with the following API code:
ThisApplication.Documents.Open(fullDocumentPathName, True)
I think that will also make it the active document.
If the document is a subassembly, you might want to open it in a specific level of detail. To do that, add the level of detail name to the end of the path name, like this:
ThisApplication.Documents.Open("C:\Models\SubAssemblyA<LevelofDetail1>", True)
Hi mhatfield,
Here's another example that demonstrates how to set an iproperty for each part in an assembly, just in case it comes in handy for your overall task.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
'Define the open document Dim openDoc As Document openDoc = ThisDoc.Document Dim docFile As Document 'kAssemblyDocumentObject = 12291 If openDoc.DocumentType = 12291 Then 'Iterate though the part files in the assembly For Each docFile In openDoc.AllReferencedDocuments 'kPartDocumentObject = 12290 If docFile.DocumentType = 12290 Then 'format file name Dim FNamePos As Long FNamePos = InStrRev(docFile.FullFileName, "\", -1) Dim docFName As String docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos) 'toggle description value If iProperties.Value(docFName, "Project", "Description") <> "On" Then 'set description iProperties.Value(docFName, "Project", "Description") = "On" else if iProperties.Value(docFName, "Project", "Description") = "On" Then 'set description iProperties.Value(docFName, "Project", "Description") = "Off" End If End If Next Else MessageBox.Show("You must have a valid Assembly document open before using this code!", "File Type Mismatch!") End If
Curtis,
Instead of using the numeric constants, you can use the enum definitions by adding the type name:
If openDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject
Also, this will work in iLogic and VB.NET code:
docFName = IO.Path.GetFileName(docFile.FullFileName)
Thanks Mike!
Thank you Mike and Curtis!
The active document code you provided did work with completing the task as well, and probably is a better idea, as then I know the add-in will recognize which document to open for, rather then opening prematurly for the assembly if the specific part has not opened yet.
That was going to be my next task Curtis, your a mind reader! Very helpful information. I've almost worked out all of the kinks in the model itself, and am moving on to the form and drawing soon. This iProperties code will definitly be used!
Thanks again for all your help
Mitch
Yijiang,
Why wouldn't you just use SendKeys?
Public Sub TestSendKeys() SendKeys "{e}" End Sub
@Yijiang.Cai/Autodesk wrote:Please create one part document first. When copying the code lines below in VBA Editor dialog and running macro, it will work fine, and this simulates to press the "E" key to create extrusion.
Thanks,
River
Option Explicit
Declare Function PostMessage& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const WM_KEYDOWN As Long = &H100
Private Const WM_KEYUP = &H101
Sub Main()
Dim FT_hWnd As Long
FT_hWnd = FindWindow(vbNullString, ThisApplication.Caption)Call PostMessage(FT_hWnd, WM_KEYDOWN, Asc("E"), 0&)
Call PostMessage(FT_hWnd, WM_KEYUP, Asc("E"), 0&)
End Sub
I run SendKeys with the Following setup:
- Windows 7 bit
- Inventor 2012 bit
with VBA and I get the error: 70 - Permission denied.