Author Topic: Search for text in all drawings in folder  (Read 3038 times)

0 Members and 1 Guest are viewing this topic.

bparent

  • Guest
Search for text in all drawings in folder
« on: December 07, 2007, 08:27:11 AM »
I often need to search for text in all drawings in a folder against an equipment list in excel or access. or even as a user prompt. I know this can be done with Ojbectdbx in vba. Would Objectdbx be the way to do this in Net or is there an easier way? Thanks.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Search for text in all drawings in folder
« Reply #1 on: December 07, 2007, 12:08:40 PM »
No need to use ObjectDBX with .Net.  All you have to do is open the drawings database, and then search that for the text you want.

This thread will help you out.  Start at this post, and continue till the end.

Hope that helps.

Ps.  Welcome to theSwamp.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8743
  • AKA Daniel
Re: Search for text in all drawings in folder
« Reply #2 on: December 07, 2007, 01:19:25 PM »
Not too hard with .NET  here is a quickly done routine, that probably does not represent best coding practices,
nor does it search paperspace, nor does it search blocks etc..etc.. but it might give you a start.  :laugh:


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

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;

      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;

      try
      {
        foreach (string pth in GetDwgFilesFromDir(prs1.StringResult))
        {
          using (Database db = new Database(false, true))
          {
            db.ReadDwgFile(pth, 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()))
                  {
                    ed.WriteMessage("\n" + pth + " contains string: " + prs2.StringResult);
                  }
                }
              }
            }
            db.CloseInput(true);
          }
        }
      }
      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, 01:26:29 AM by Daniel »

bparent

  • Guest
Re: Search for text in all drawings in folder
« Reply #3 on: December 10, 2007, 07:55:52 AM »
Thanks for the thread link. I see Net can do what I need without Ojbetdbx and will continue to study and research the file and directory iteration with Net.

No need to use ObjectDBX with .Net.  All you have to do is open the drawings database, and then search that for the text you want.

This thread will help you out.  Start at this post, and continue till the end.

Hope that helps.

Ps.  Welcome to theSwamp.

bparent

  • Guest
Re: Search for text in all drawings in folder
« Reply #4 on: December 10, 2007, 07:58:39 AM »
Thanks. This will give me a start. I downloaded and installed Net 3.5 but am getting error with the code segment requiring 3.5. The errors are about expected ";" and illegal "in", "select", etc. I suspect references are needed for Linq?

Not too hard with .NET  here is a quickly done routine, that probably does not represent best coding practices,
nor does it search paperspace, nor does it search blocks etc..etc.. but it might give you a start.  :laugh:


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

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;

      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;

      try
      {
        foreach (string pth in GetDwgFilesFromDir(prs1.StringResult))
        {
          using (Database db = new Database(false, true))
          {
            db.ReadDwgFile(pth, 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()))
                  {
                    ed.WriteMessage("\n" + pth + " contains string: " + prs2.StringResult);
                  }
                }
              }
            }
            db.CloseInput(true);
          }
        }
      }
      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();
    }
  }
}


It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8743
  • AKA Daniel
Re: Search for text in all drawings in folder
« Reply #5 on: December 10, 2007, 08:10:32 AM »
You will need VS2008 to compile it. I have attached the .DLL for you so you can have try.

http://www.microsoft.com/express/