TheSwamp

Code Red => .NET => Topic started by: MickD on May 14, 2009, 11:58:54 PM

Title: [IronPython] Using C# derived form in your Ipy script
Post by: MickD on May 14, 2009, 11:58:54 PM
As mentioned in other threads and elsewhere, it's possible to create your winforms dialogs etc in VS or Sharpdevelop so you have a nice ide to place and arrange controls with. Just compile the class lib and import it into your Ipy module.
You then need to derive from it and add your event handlers and change properties as required.

Here's a quick example based on Daniel's count layers demo.

Code: [Select]
import sys
sys.path.append('C:\\Program Files\\AutoCAD 2009')
sys.path.append('D:\\_Dev\\IPyCADv1.1\\IPyCADv1.1\\bin\\Debug')
sys.path.append('C:\\DCS_3D\\ipyscripts')
import clr
clr.AddReferenceToFile('acdbmgd.dll')
clr.AddReferenceToFile('acmgd.dll')
clr.AddReferenceToFile('IpyForm.dll')

from IpyForm import *
import Autodesk.AutoCAD.DatabaseServices as dbs
import Autodesk.AutoCAD.ApplicationServices.Application as app

class My_form(IpyForm):
        def __init__(self):
                IpyForm.__init__(self)
                self.Text = "IronPython Demo"
                self.Controls["label1"].Text = "Wow it's Python"
                self.Controls["label2"].Text = "0"
                self.Controls["button1"].Text = "Count Layers"
                self.Controls["button2"].Text = "Close"
                self.Controls["button1"].Click += self.lay_click
                self.Controls["button2"].Click += self.close_click

        def count_layers(self):
                try:
                        db = dbs.HostApplicationServices.WorkingDatabase
                        print db.ToString()
                        tm = db.TransactionManager
                        tr = tm.StartTransaction()
                        ltbl = tm.GetObject(db.LayerTableId, dbs.OpenMode.ForRead, False)
                        i = 0
                        for layer in ltbl:
                                i = i + 1
                        tr.Commit()
                        tr.Dispose()
                        return i       
                except:
                        tr.Dispose()
                        print "error"

        def close_click(self,*args):
                self.Close()

        def lay_click(self,*args):
                self.Controls["label2"].Text = str(self.count_layers())


my_form = My_form()
app.ShowModalDialog(my_form)


And the piccy of the form as designed in VS2005
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: It's Alive! on May 15, 2009, 12:02:49 AM
Pretty slick!  8-)
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: MickD on May 15, 2009, 12:23:02 AM
Yep, pretty simple :)
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: It's Alive! on May 15, 2009, 07:25:47 AM
What do you guys think about BOO?
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: MP on May 15, 2009, 10:01:17 AM
I think BOO is very cool (Tim Wiley introduced us to it looooooong ago). The only thing that concerns me is wasted development investment should it wither and die.

PS: Nice demo Mick. :)
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: Spike Wilbury on May 15, 2009, 10:09:21 AM
I think BOO is very cool (Tim Wiley introduced us to it looooooong ago). The only thing that concerns me is wasted development investment should it wither and die.

PS: Nice demo Mick. :)

Wasn't other Tim now using other identity: r  ?
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: MP on May 15, 2009, 10:14:40 AM
I dunno, maybe he goes by Ringo -- he does march to a different drummer.

(http://www.theswamp.org/screens/mp/facepalm.gif)
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: Spike Wilbury on May 15, 2009, 10:16:56 AM
I dunno, maybe he goes by Ringo -- he does march to a different drummer.

(http://www.theswamp.org/screens/mp/facepalm.gif)

He he.....
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: It's Alive! on May 15, 2009, 10:18:38 AM
Seems a good way to go, its statically typed and supports attributes, no fuss, no muss, just netload and run.
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: MP on May 15, 2009, 10:20:20 AM
the clean python like syntax / syntactic sugar is very appetizing and surprisingly lo cal :D
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: It's Alive! on May 15, 2009, 10:24:57 AM
Syntactic Sucralose :|
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: MP on May 15, 2009, 10:27:00 AM
wow, "boo seems good" to "boo will give you cancer" in just a couple posts; i be confused
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: It's Alive! on May 15, 2009, 10:29:27 AM
 :laugh:
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: It's Alive! on May 15, 2009, 10:34:50 AM
Installing BoolLangStudio now
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: It's Alive! on May 15, 2009, 11:19:22 AM
nope BooLangStudio is DOA with VC2008sp1  too bad
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: MP on May 15, 2009, 11:30:43 AM
SharpDevelop FTW (see "Languages Supported") (http://community.sharpdevelop.net/blogs/mattward/articles/VisualStudioExpressComparison.aspx)

 :)
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: MP on May 15, 2009, 11:34:35 AM
As an aside, you may find this interesting too. (http://community.sharpdevelop.net/blogs/mattward/archive/2009/05/11/ConvertingCSharpVBNetCodeToIronPython.aspx)

:)
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: MickD on May 16, 2009, 05:31:07 AM
I think BOO is very cool (Tim Wiley introduced us to it looooooong ago). The only thing that concerns me is wasted development investment should it wither and die.

PS: Nice demo Mick. :)

Thanks.

Call me old fashioned but I just like Python :)

I haven't looked at Boo too much but I don't think Python is going anywhere soon and it's working well with .net so I'll stick with it for a while I think.
Ideally I'd like to hide the .net interface in helper modules that I can replace with C arx extension modules at a later date if I need more speed or to lock down some of the source code, that's the plan anyway :)
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: tjr on May 18, 2009, 08:08:16 PM
I dunno, maybe he goes by Ringo -- he does march to a different drummer.

(http://www.theswamp.org/screens/mp/facepalm.gif)
Thanks. :)
Title: Re: [IronPython] Using C# derived form in your Ipy script
Post by: tjr on May 18, 2009, 08:12:31 PM
Ideally I'd like to hide the .net interface in helper modules that I can replace with C arx extension modules at a later date if I need more speed or to lock down some of the source code, that's the plan anyway :)
If you're looking to hide some source code I believe you can compile to a .dll using pyc.py and then import it into IronPython. Don't hold me to this though.