Author Topic: [IronPython] Selection set example  (Read 3251 times)

0 Members and 1 Guest are viewing this topic.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
[IronPython] Selection set example
« on: May 07, 2009, 07:57:09 PM »
Hi All,
I have a bit of work to do with Ipy and I thought I'd share some snippets as I go.

This is probably not the best implementation but it shows a couple of important things to consider when dealing with .net from Python, in this case passing an array of objects (Python doesn't have arrays).

The method is basically some boiler plate code for selecting some objects of a particular type, it wouldn't be hard to extend this for multiple types if needed.

You will need Tim's PyAcad.Net to run this in acad, if you need a simple dll just to run scripts I will be posting a simple VS solution in another thread which I'll link in here at a later date.

The python script:
Code: [Select]
# selection.py
# Created: 7th May 2009 Copyright Mick Duprez
# initialise and import our lib's

import sys
#----- change these paths as required for your system and lib.file locations
sys.path.append('C:\\Program Files\\AutoCAD 2009')
sys.path.append('C:\\DCS_3D\\ipyscripts')
import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
clr.AddReferenceToFile('acdbmgd.dll')
clr.AddReferenceToFile('acmgd.dll')
import System.Windows.Forms as winfrm
from System.Drawing import *
import Autodesk.AutoCAD.DatabaseServices as acdb
import Autodesk.AutoCAD.ApplicationServices.Application as app
import Autodesk.AutoCAD.EditorInput as aced
from System import Array
# method to select entities using a simple filter for entity type
def select_ents(typecode, value):
        doc = app.DocumentManager.MdiActiveDocument
        ed = doc.Editor
        tv = acdb.TypedValue(typecode, value)
        filter = Array[acdb.TypedValue]([tv]) # create a .net array for acad to use
        res = ed.GetSelection(aced.SelectionFilter(filter))
        if res.Status != aced.PromptStatus.OK or res.Status == aced.PromptStatus.Cancel:
                return None
        ids = res.Value.GetObjectIds() # gives us an array object
        return ids

#### test the selection method:
ids = select_ents(0, "3DSOLID")
doc = app.DocumentManager.MdiActiveDocument
ed = doc.Editor
idlist = list(ids) #convert the array to a python list
for id in idlist:
        ed.WriteMessage(id.ToString() + '\n')

PS -  for IronPython 1.1, acad 2007-9 :)
« Last Edit: May 07, 2009, 08:14:21 PM by MickD »
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

HOSNEYALAA

  • Newt
  • Posts: 103
Re: [IronPython] Selection set example
« Reply #1 on: July 06, 2021, 09:53:17 AM »
THANK YOU  @MickD

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: [IronPython] Selection set example
« Reply #2 on: July 06, 2021, 05:52:38 PM »
No problem, a bit old now but hopefully useful, cheers. :)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien