Author Topic: Microsoft Parallel Extensions to .NET (CTP)  (Read 2358 times)

0 Members and 1 Guest are viewing this topic.


It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8743
  • AKA Daniel
Re: Microsoft Parallel Extensions to .NET (CTP)
« Reply #1 on: December 08, 2007, 02:52:33 AM »
aahhhhhhhhhhh, its not fair!

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;

using AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcRx = Autodesk.AutoCAD.Runtime;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcWd = Autodesk.AutoCAD.Windows;

[assembly: CommandClass(typeof(search.Commands))]

namespace search
{
  public static class Commands
  {
    [CommandMethod("doit")]
    public static void doit()
    {
      Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;

      List<string> strlist = new List<string>();

      PromptStringOptions pso1 = new PromptStringOptions("Enter path");
      pso1.AllowSpaces = true;
      PromptResult prs1 = ed.GetString(pso1);
      if (prs1.Status != PromptStatus.OK)
        return;

      PromptStringOptions pso2 = new PromptStringOptions("Enter String to search");
      pso2.AllowSpaces = true;
      PromptResult prs2 = ed.GetString(pso2);
      if (prs2.Status != PromptStatus.OK)
        return;

      string[] strarray = GetDwgFilesFromDir(prs1.StringResult);

      try
      {
        Parallel.For(0, strarray.Length, delegate(int i) //<----------------------------
        {
          using (Database db = new Database(false, true))
          {
            db.ReadDwgFile(strarray[i], FileShare.Read, true, "");
            AcDb.TransactionManager Tm = db.TransactionManager;

            using (Transaction tr = Tm.StartTransaction())
            {
              BlockTable tb = (BlockTable)tr.GetObject
                           (db.BlockTableId, OpenMode.ForRead, false);

              BlockTableRecord btr = (BlockTableRecord)tr.GetObject
                           (tb[BlockTableRecord.ModelSpace], OpenMode.ForRead, false);

              foreach (ObjectId id in btr)
              {
                DBObject dboject = (tr.GetObject(id, OpenMode.ForRead));

                if (dboject is MText)
                {
                  MText mt = (MText)dboject;
                  if (mt.Text.ToUpper().Contains(prs2.StringResult.ToUpper()))
                  {
                    strlist.Add(strarray[i] + " contains string: " + prs2.StringResult);
                  }
                }
              }
            }
            db.CloseInput(true);
          }
        });

        foreach (string str in strlist)
        {
          ed.WriteMessage("\n" + str);
        }
      }
      catch (System.Exception ex)
      {
        ed.WriteMessage(ex.Message);
      }
    }

    //this function needs .NET 3.5 to run/compile
    public static string[] GetDwgFilesFromDir(string path)
    {
      var MyList = from f in Directory.GetFiles((path))
                   where Path.GetFileName(f).ToUpper().Contains(".DWG")
                   select f;
      return MyList.ToArray();
    }
  }
}

« Last Edit: December 08, 2007, 03:02:45 AM by Daniel »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Microsoft Parallel Extensions to .NET (CTP)
« Reply #2 on: December 08, 2007, 03:22:58 AM »

Looks like you've had a good Saturday afternoon Daniel :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8743
  • AKA Daniel
Re: Microsoft Parallel Extensions to .NET (CTP)
« Reply #3 on: December 08, 2007, 03:35:12 AM »

Looks like you've had a good Saturday afternoon Daniel :-)
Yes yes, I am now able to throw exceptions in parallel  :lol: