Community
Inventor Forum
Welcome to Autodesk’s Inventor Forums. Share your knowledge, ask questions, and explore popular Inventor topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Spell a word on VBA

19 REPLIES 19
Reply
Message 1 of 20
nikhunter
645 Views, 19 Replies

Spell a word on VBA

Hello to everyone.For on more time i want your help for a problem that i face


I just wanna do the follownig.

 

I want to create a label with a textbox (myabe) in which i will write by my keyboard a word.After that i want to press the "Enter" button and the programm do this simple thing...."SPELL" each letter of my word, an activate a specific command each time and in my case a specific RULE from iLogic.

 

For example, i wanna whrite my name.....NIKOS....and after pressing the ENTER button, firstly my programm read the letter "N" and trigger the Rule1....After a specific time (that i want to defined by myself, probably with a timer) the programm read the right next letter which is "I" and activate the next Rule2...Until the last letter where it will end.

 

I should note here that i already have the code that i can calling as Rule i wish.But i dont know how to use it for my job....

 

The code is the following which I had gotten from someone of here...

 

 

 

Dim iLogicAuto As Object
Set iLogicAuto = GetiLogicAddin(ThisApplication)
If (iLogicAuto Is Nothing) Then Exit Sub

Dim doc As Document
Set doc = ThisApplication.ActiveDocument

Dim ruleName As String
ruleName = "Rule1"
Dim rule As Object
Set rule = iLogicAuto.GetRule(doc, "Rule1")
If (rule Is Nothing) Then
Call MsgBox("No rule named " & ruleName & " was found in the document.")
Exit Sub
End If

Dim i As Integer
i = iLogicAuto.RunRuleDirect(rule)

 

 

 

 

 

Thanks and i apriciate your help

 

19 REPLIES 19
Message 2 of 20
mrattray
in reply to: nikhunter

I'm not sure I understand what you're trying to do, but I'll take a shot. This assumes that what you posted works.

 

 

Public Sub readInput

Dim letter As String
Dim word As String

'Creates a prompt for you to type a word into and assigns it to the variable "word".
word = InputBox("Enter your word.", vbOkCancel, "Your Title")

'Loops through each character in the word you entered.
For i = 1 to Len(word)

'Sets the variable "letter" to each character in "word"
letter = Mid(word, i, 1)
'Fires sub "runRule" and feeds it the variable "letter". This will fire once for every character in "word"
runRule(letter)

Next

End Sub

Private Sub runRule (letter As String)

Dim iLogicAuto As Object
Set iLogicAuto = GetiLogicAddin(ThisApplication)
If (iLogicAuto Is Nothing) Then Exit Sub

Dim doc As Document
Set doc = ThisApplication.ActiveDocument
Dim rule As Object
Dim ruleName As String
Dim i As Integer

Select Case letter
Case Is = "A"
ruleName = "Rule1"
Case Is = "B"
ruleName = "Rule2"
Case Else
  msgbox("Invalid Entry",vbokonly,"Error")
End Select

Set rule = iLogicAuto.GetRule(doc, ruleName)
If (rule Is Nothing) Then
Call MsgBox("No rule named " & ruleName & " was found in the document.")
Exit Sub
End If

i = iLogicAuto.RunRuleDirect(rule)

End Sub

 

Note that I wrote this in a text file and didn't test it at all or use any references besides my memory... there may be errors. Smiley Happy

Mike (not Matt) Rattray

Message 3 of 20
nikhunter
in reply to: nikhunter

First of all thanks for your imidiate response...

 

I must tell you that it works but not completely...The failure is that after of activation of each Rule, i want to see its result....in this case i see only the last result....

 

For example i have a circle...Rule1 cut it in half and Rule2 delete one of the half part....

 

Now.With your code, firstly i see the hole circle and after that i see directly the half....Commonly, the code runs from the back and showing me only the last stage....

 

What I want to do is to watch every stage. I mean firt the hole circle, after that the cutting and finally the half....

But all this its must be doing after pressing the button "OK" automation...I wanna have the picture of each situation...

However thanks for your HELP.It's valuable...

 

Message 4 of 20
mrattray
in reply to: nikhunter

If I'm understanding correctly, you want the code to pause in between each rule and wait for the user to click a "continue" button. If I'm right, then this is very easy. Just add the following line to the previous code just before the second End Sub, so this will be the second from very last line in the code.

 

Msgbox("Press OK to continue.",vbokonly,"Continue?")

 

Mike (not Matt) Rattray

Message 5 of 20
nikhunter
in reply to: nikhunter

Ok i'll try one more last time, and i wish i can give you the point....

 

I actually want to do the last thing you say BUT, without wait the user to click any button...

 

the operation must be gone automation...i press the firstly button "OK" and then the programm read all the letters of my word one by one and activate each Rule corresponds to each letter.

 

I think that my problem is to have a command like "me.refresh" in the loop, which through me out after read any letter, give me the image of my work, and after that puss me back to read the next letter and do the same thing...I try to explain that after each reading some letters I want to have the picture....Ιn my case i just have the last image...I dοnt observe the intermediate stage but only the last one

 

Ι cannot explain better. Anyway i use all of your advise and i hope to fix it out....THANKS again...

Message 6 of 20
mrattray
in reply to: nikhunter

I think I understand what you're looking for. I think you're trying to update your display after each rule has been run. If that's correct then you should be able to use this line to accomplish it:

 

 

doc.Update

 

Just insert this in the same spot I suggested inserting the message box earlier, just before the second End Sub. You can combine this with the message box line if you wish (make sure that you put the update line before the message box). This way the user can have a moment to review the results of each rule before continuing.

Mike (not Matt) Rattray

Message 7 of 20
nikhunter
in reply to: nikhunter

Yeah finally you got the point but it's not still working....

 

At the previous case, and i mean that with the msgbox which placed up to the second "End Sub", everything was ok and the programm run perfect. I had the result you expected from this command....

 

But now, i see again the final stage directly without any intermediate point. I think that's problem about the command "doc.Update"...maybe some other type of command to work.

 

I think i tired you....

Message 8 of 20
mrattray
in reply to: nikhunter

It doesn't work even with both lines added? Your code should look like this:

 

Public Sub readInput

Dim letter As String
Dim word As String

'Creates a prompt for you to type a word into and assigns it to the variable "word".
word = InputBox("Enter your word.", vbOkCancel, "Your Title")

'Loops through each character in the word you entered.
For i = 1 to Len(word)

'Sets the variable "letter" to each character in "word"
letter = Mid(word, i, 1)
'Fires sub "runRule" and feeds it the variable "letter". This will fire once for every character in "word"
runRule(letter)

Next

End Sub

Private Sub runRule (letter As String)

Dim iLogicAuto As Object
Set iLogicAuto = GetiLogicAddin(ThisApplication)
If (iLogicAuto Is Nothing) Then Exit Sub

Dim doc As Document
Set doc = ThisApplication.ActiveDocument
Dim rule As Object
Dim ruleName As String
Dim i As Integer

Select Case letter
Case Is = "A"
ruleName = "Rule1"
Case Is = "B"
ruleName = "Rule2"
Case Else
  msgbox("Invalid Entry",vbokonly,"Error")
End Select

Set rule = iLogicAuto.GetRule(doc, ruleName)
If (rule Is Nothing) Then
Call MsgBox("No rule named " & ruleName & " was found in the document.")
Exit Sub
End If

i = iLogicAuto.RunRuleDirect(rule)

'Check the results
doc.Update
Msgbox("Press OK to continue.",vbokonly,"Continue?")

End Sub

 

This should, after each rule has been run, update the display and wait for OK to be clicked before continuing on to the next rule.

 

I'm not growing tired of you, I'm just having a hard time understanding what you're looking for.

Mike (not Matt) Rattray

Message 9 of 20
GSE_Dan_A
in reply to: mrattray

Mike,


I think he wants the rules to fire without having to press OK in between each.  So once OK is initally pressed, the first rule runs, shows the letter. The next rule runs, shows the next letter, and so on, until the complete word is displayed.

GSE Consultants Inc.
Windsor, ON. Canada
Message 10 of 20
SeanFarr
in reply to: nikhunter


@nikhunter wrote:

 

But now, i see again the final stage directly without any intermediate point. I think that's problem about the command "doc.Update"...maybe some other type of command to work.

 


I think he wants the rule to run with no user input once enter is pressed initially, but the the rules run so fast, there is no pause in between each rule, can you put a timer in between each rule? So that each rule can be seen, 5 seconds or something between each rule ran?

 

I have no VBA knowledge, but this is interesting...

 

Sean

Sean Farr
Product Designer at TESInc.ca

Inventor Professional 2014-Update 2 - AutoCAD Electrical 2014
Win7-x64 | ASUS P8Z77-V | i7 3770 -3.4 GHz | 32GB RAM |
240GB SSD | nVidia GTX 670 4GB - 320.49
Message 11 of 20
mrattray
in reply to: SeanFarr

Thanks for jumping in with the clarification Dan & Sean.

 

After re-reading the whole thread, I think Sean may be on the right track. Unfortunately, I don't have an immediate solution for him that would work in Inventor's API interface. It would be trivial in VB. If I can get a confirmation from Nikos that this is what he is after, then I'll play around with it later this afternoon after I get some of my work done.

Programming a timer in VBA is something that's been on my wish list for figuring out so, this is indeed a bit interesting. I'm really curious what Nikos is actually trying to accomplish with this in the bigger picture.

Mike (not Matt) Rattray

Message 12 of 20
nikhunter
in reply to: nikhunter

Ok now i'm sure that all of you, have got the right point on what i'm trying to do

 

Dan, you are exactly on my thoughts....Sean, i have been thinking that way, to use a timer, but i can't find it on VBA....I mean a timer application....

 

mrattray your first command (msgbox....) works very well, and after each reading of a letter,I have the intermediate picture, and a pause with the specific label, which waiting by me to press the button ok and read the second letter....BUT, in this case, i have this "pause label", which waiting my intervention,  that i try to avoid and replace it by an automation application which give me a few seconds to show the right icon and go next ...

 

At the second command (doc.Update) i dont have any indermidiate picture, and i watch directly the finally depiction...

 

Yeah i think that a timer would be the better way to encounter my problem..Between each reading a letter, the program will activate a delay by the timer, that will give me the possibility to see the intermidiate stage...And after passing the specific time that i will have define, the program will reading the next letter and will doing the same work...So i just wanna the right code that doing this delay and on that pause i have the desired image and after passing the time go next...

Message 13 of 20
mrattray
in reply to: nikhunter

OK Nikos, I understand what you're after now. I just need to find some time in between jobs that I can figure something out for you.

Mike (not Matt) Rattray

Message 14 of 20
nikhunter
in reply to: nikhunter

mrattray thanks a lot for your time and i really appreciate that...

 

Take as much time as you want, cause i don't want to disturb you from your occupations...

 

I'll be waiting for your correspondence when you have some new stuff...

Message 15 of 20
mrattray
in reply to: nikhunter

Try this out. This should wait 3 seconds between running rules. You can change it to whatever you like. Let me know how it works for you.

 

'Windows call
Private Declare Sub AppSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
 
Public Sub PauseApp(PauseInSeconds As Long)
    
    Call AppSleep(PauseInSeconds * 1000)
    
End Sub

Public Sub readInput

Dim letter As String
Dim word As String

'Creates a prompt for you to type a word into and assigns it to the variable "word".
word = InputBox("Enter your word.", vbOkCancel, "Your Title")

'Loops through each character in the word you entered.
For i = 1 to Len(word)

'Sets the variable "letter" to each character in "word"
letter = Mid(word, i, 1)
'Fires sub "runRule" and feeds it the variable "letter". This will fire once for every character in "word"
runRule(letter)

'This causes the code to pause for the specified number of seconds
PauseApp 3

Next

End Sub

Private Sub runRule (letter As String)

Dim iLogicAuto As Object
Set iLogicAuto = GetiLogicAddin(ThisApplication)
If (iLogicAuto Is Nothing) Then Exit Sub

Dim doc As Document
Set doc = ThisApplication.ActiveDocument
Dim rule As Object
Dim ruleName As String
Dim i As Integer

Select Case letter
Case Is = "A"
ruleName = "Rule1"
Case Is = "B"
ruleName = "Rule2"
Case Else
  msgbox("Invalid Entry",vbokonly,"Error")
End Select

Set rule = iLogicAuto.GetRule(doc, ruleName)
If (rule Is Nothing) Then
Call MsgBox("No rule named " & ruleName & " was found in the document.")
Exit Sub
End If

i = iLogicAuto.RunRuleDirect(rule)

End Sub

 

Mike (not Matt) Rattray

Message 16 of 20
nikhunter
in reply to: nikhunter

Ok. Now i have the desirability delay but it still doesn't display to me the indermidiate picture....Propably because i need one more command to do the update except for the pause...

 

 

The only code which have been working until now, was the Msgbox("Press OK to continue.",vbokonly,:Continue?") which  added just before the second End Sub... With this line i have the result i want, but with my intervention.I think that this happen because the specific line cause a break to the programm and it can display the last operation which is to read each Rule...

 

Needed some command that makes update the picture after the "delay function".Something like the "doc.Update" but which works correct....

Message 17 of 20
mrattray
in reply to: nikhunter

Try it with both the update method and the pause sub:

 

'Windows call
Private Declare Sub AppSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
 
Public Sub PauseApp(PauseInSeconds As Long)
    
    Call AppSleep(PauseInSeconds * 1000)
    
End Sub

Public Sub readInput

Dim letter As String
Dim word As String

'Creates a prompt for you to type a word into and assigns it to the variable "word".
word = InputBox("Enter your word.", vbOkCancel, "Your Title")

'Loops through each character in the word you entered.
For i = 1 to Len(word)

'Sets the variable "letter" to each character in "word"
letter = Mid(word, i, 1)
'Fires sub "runRule" and feeds it the variable "letter". This will fire once for every character in "word"
runRule(letter)

Next

End Sub

Private Sub runRule (letter As String)

Dim iLogicAuto As Object
Set iLogicAuto = GetiLogicAddin(ThisApplication)
If (iLogicAuto Is Nothing) Then Exit Sub

Dim doc As Document
Set doc = ThisApplication.ActiveDocument
Dim rule As Object
Dim ruleName As String
Dim i As Integer

Select Case letter
Case Is = "A"
ruleName = "Rule1"
Case Is = "B"
ruleName = "Rule2"
Case Else
  msgbox("Invalid Entry",vbokonly,"Error")
End Select

Set rule = iLogicAuto.GetRule(doc, ruleName)
If (rule Is Nothing) Then
Call MsgBox("No rule named " & ruleName & " was found in the document.")
Exit Sub
End If

i = iLogicAuto.RunRuleDirect(rule)

doc.Update

'This causes the code to pause for the specified number of seconds
PauseApp 3

End Sub

 

If this doesn't work, then I'll need to know what it is you're doing with the rules. That way I can drill directly into the object to update it.

Mike (not Matt) Rattray

Message 18 of 20
nikhunter
in reply to: nikhunter

Grrrrr....It's still doesn't work....

 

Ok with a Rule i change some degrees on a constraint angle...For instance i have doing a constraint angle at two objects, and when i run first rule (Rule1) the degrees are 90 and when i activate the second (Rule2) the degrees change to 180.....But my work is more complicated...

 

This is the Rule code which i modified according to each angle i want in any Rule....

 

 

Parameter("d248") = 180
Constraint.IsActive("Angle:1") = True

 

Say for example i want to read the word "NIKOS"....When the programm spell the first letter which is "N", the first Rule will be activated and give me the right angle i desire....But i want to see this change....After 3 sec i read the next letter and the second Rule will be activated and give me another angle....And i want to see that again....And this will be continued still the final letter....In our case, i see only the final angle...

Message 19 of 20
mrattray
in reply to: nikhunter

These things always take some trial and error. Usually you can put a strong emphasis on the error. 

 

I was under the impression that we were working in a drawing. What happens if you add this line to the beginning of all of your iLogic rules? (You can test it in just a couple if you want to avoid editing a ton of rules for something that might not wok)

 

iLogicVb.UpdateWhenDone = True

 

You might also try this one. Note that the above line will work anywhere in the rule, but the following line needs to be at the end.

 

InventorVb.DocumentUpdate()

 

Are we, by chance, dealing with sub assemblies or iParts/iAssemblies? Is there anything else you can think of with your assembly that might be causing conflicts with Inventor updating properly?

 

Edit in response to your edit: I know what you're trying to achieve, the code snippets that I've been posting over the last few replies should be doing just that. Now we have to do a bit of debugging to figure out why it's not working the way it should.

 

Mike (not Matt) Rattray

Message 20 of 20
nikhunter
in reply to: nikhunter

YEEEEEEEEAAAAAAAAAAHHHHHHH   Smiley Tongue

 

Ιt worked with the second command....I have the desired result

 

 

Man i really appriciate your HELP and i hope to continue that good work......I dont know if i tired you but your patience it was worth it.Thanks again.Really thank you...!!!!!!!!!

 

InventorVb.DocumentUpdate()

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report