Sign in to follow this  
Followers 0
taietel

Chemical formulas

8 posts in this topic

Is there a way to write subscripts in AutoIt (something like H3C-CH3 )?

I want to make a program that will automatically correct the chemical formulas, as I type, in an edit box or rich text box.

If there is, please point me in the right direction.

Thank you in advance!

Share this post


Link to post
Share on other sites



Is there a way to write subscripts in AutoIt (something like H3C-CH3 )?

I want to make a program that will automatically correct the chemical formulas, as I type, in an edit box or rich text box.

If there is, please point me in the right direction.

Thank you in advance!

That's an attribute of the software displaying the text. The method to do that in HTML for a web page, IPB for a web forum, or ODF for OpenOffice are all going to be different. What app/file format do you want to do this in?

:idea:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

First I've thought at rtf, but html is an alternative. Either way, I appreciate the help!

The hard part is how to get the number in subscript... Maybe to send some keys? I don't know if it works in a richtextbox, but the combination CTRL+"+" works in Wordpad and Word, for subscripts.

The ideea is that as user types, the script checks if a space follows a number. If it is, then checks the number's lenght (characters) and turn the number into subscript, then move the cursor after space.

 [edit] In the documentation is a function, _GUICtrlRichEdit_SetCharAttributes, but the attribute sb do not display.   :)

[edit] How can I get the text as I type in a variable? In the example below

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GuiRichEdit.au3>
Opt("GUIOnEventMode", 1)
$hGUI = GUICreate("hGUI", 633, 369, 192, 124)

$hRichEdit = _GUICtrlRichEdit_Create($hGui, "", 8, 8, 617, 297, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))

GUICtrlSetOnEvent($hRichEdit, "_RichEditChange")

GUISetState(@SW_SHOW)

While 1
    Sleep(10)
WEnd



Func _GUIClose()
     GUIDelete($hGUI)
     Exit
EndFunc

Func _RichEditChange()
     ConsoleWrite(GUICtrlRead($hRichEdit))
EndFunc

nothing happens if I type.  :idea: I have tried also  _GUICtrlRichEdit_GetText($hRichEdit). 

I've found that RichEdit did catch the combination "CTRL+" for changing to subscript.

Edited by taietel

Share this post


Link to post
Share on other sites

With the help from the documentation I came up to this:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $Text
$Form1 = GUICreate("Form1", 605, 284)
GUICtrlCreateLabel("Type some text and write simple chemical reaction or press the button", 8, 8)
$Edit1 = GUICtrlCreateEdit("", 8, 40, 585, 161, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN,$WS_HSCROLL,$WS_VSCROLL,$WS_BORDER))
$ftest = "This is a test" & @CRLF
$ftest &= @TAB & "3Ca(OH)2 + 2H3PO4 --> Ca3(PO4)2 + 6H2O" & @CRLF
$ftest &= @TAB & "H3C-CH=CH2 + HCl --> H3C-CHCl-CH3" & @CRLF
$ftest &= "(tip: check the simbols - this is not implemented yet and I did it to see if it works)" & @CRLF
$ftest &= "create an empty document (.doc) first, in the script directory."
GUICtrlSetData($Edit1, $ftest)
$Button1 = GUICtrlCreateButton("Button", 400, 216, 193, 57, $WS_GROUP)
GUISetState(@SW_SHOW)

While 1
    Sleep(10)
 $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $sText = StringSplit(GUICtrlRead($Edit1),"",2)
            ShellExecute(@ScriptDir & "\test.doc")
            WinWaitActive("test")
            For $i = 0 To UBound($sText)-1
                Switch $i
                    Case 0
                        Send($sText[0],1)
                    Case 1 To UBound($sText)-1
                        If $sText[$i]=@LF Then
                            Send("{ENTER}{DEL}")
                        Else
                            Select
                                Case StringIsInt($sText[$i])=1
                                    If $sText[$i-1] = "+" Or $sText[$i-1] = " " Or $sText[$i-1]=@Lf Or $sText[$i-1]=@TAB Or StringIsInt($sText[$i-1])=1 Then
                                        Send($sText[$i],1)
                                    Else
                                        Send("{CTRLDOWN}{=}" & "{CTRLUP}" & $sText[$i] & "{CTRLDOWN}{=}" & "{CTRLUP}")
                                    EndIf
                                Case Else
                                    Send($sText[$i],1)
                            EndSelect
                        EndIf
                    Case UBound($sText)
                        Send($sText[$i])
                EndSwitch
            Next
    EndSwitch
WEnd

If the number of atoms is greater than 9, the subscript fails (for now). No problem with the compounds.

I think this is lot harder than I thought...   :idea: 

To do:

- autocorrect symbols if typed wrong (e.g. na->Na, kcl->KCl...)

- autocorrect formula if it is wrong (e.g. baso->BaSO4)

- chemical balance of reactions ...

-....

Anyone has another idea about this kind of things? Or if it was been done in Autoit? I really don't want to rediscover the boiled water...

chemical.au3

Share this post


Link to post
Share on other sites

Try this!


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

The idea is that as you start wondering more than just subscripting and are looking into well-formed equations, then a stronger approach may be the right thing to do. I don't know how powerful the add-on is, but it looks like good and freely available.

I guess that the various chemical packages available for LATEX are probably a bit complex to use for you.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I don't have any problems writing chemical reactions, formulas etc, but I have some chemistry colleagues who don't know how to write these and I want to help them. They barely write in word! That's why I want Autoit to do the job. And they also don't have Office 2007 or 2010, but previous versions.

[edit] There is a lot of chemistry software, especially for linux, and I use it a lot, but I it's hard to teach an 50-60 year old teacher computer stuff or linux...  

Edited by taietel

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0