Author Topic: Need help making a lisp function for Thumbnailer  (Read 1997 times)

0 Members and 1 Guest are viewing this topic.

BillZndl

  • Guest
Need help making a lisp function for Thumbnailer
« on: June 12, 2014, 11:23:41 AM »
R2012 VS 2010 Express.

I have a lisp program I am using to replace certain text items in a list of drawings using ODBX.
Before we upgraded to R2012, I used Tony T.'s thumbnailer.arx to retain the preview bitmap in the drawing.

This .arx no longer works in 2012 so I thought if possible, I could do the same thing with a lisp function.
But I need some guidance.
I think I would do something like the following.

Code: [Select]
public static class MyLispFunctions
          {
            [LispFunction("THUMBNAILPRESERVE")]
 
               public static void SetEntGetData(ResultBuffer rb)
                 {
                   if (rb == null)
                     return; Document doc = AcadApp.DocumentManager.MdiActiveDocument;
                     db = doc.Database;
                     db.PreserveThumbnail; ?????

                 }
           }

In my old code I did the following.
So would like to have a lisp function with arguments to do the same thing.
Code: [Select]
(defun SaveAsEx (dbxDoc filename / app)

  (setq app (vla-GetInterfaceObject
              (vlax-get-acad-object)
                 "Thumbnailer.Application"
             )
           )
         (vlax-invoke-method app 'PreserveThumbnail        ;(vlax-invoke-method app 'PreservePreview
             (vlax-get-property dbxDoc 'Database)
           )
         (vlax-release-object app)
         (vlax-invoke-method dbxDoc 'SaveAs filename)
 )

Thanks in advance!

Bill

Keith Brown

  • Swamp Rat
  • Posts: 601
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

BillZndl

  • Guest
Re: Need help making a lisp function for Thumbnailer
« Reply #2 on: June 12, 2014, 04:01:38 PM »
Hmm, ok.

I've looked at quite a few ways to attempt this but, it runs and leaves even a file that has a bitmap blank.
Seems like my trouble is that I can't find the Document of the remote DataBase to get the original Bitmap from.
I made it a lisp function so I can call the command with an argument.
Then execute the method to do the work.


Code: [Select]
public class ThumbnailPreserve
    {

        [LispFunction("TNSave")]
        public object TNSave(ResultBuffer resbuf)
        {
            if (resbuf == null)
                return false;

            TypedValue[] args = resbuf.AsArray();

            if (args.Length != 1)
                return false;

            TypedValue arg = args[0];
            short type = arg.TypeCode;

            if (type == (short)LispDataType.Text)
                ThumbnailSave(((string)arg.Value));

            return true;
        }
       
       
        public void ThumbnailSave(string dwgFile)
        {
            if (!string.IsNullOrEmpty(dwgFile) && File.Exists(dwgFile))
            {               

                    Database db = new Database(true, true);
                    db.ReadDwgFile(dwgFile, System.IO.FileShare.ReadWrite, false, "");
                   
                    Document doc = //??? where is document of db ???

                    Bitmap thumb = doc.CapturePreviewImage(320, 240);
                    doc.Database.ThumbnailBitmap = thumb;

                    db.SaveAs(dwgFile, DwgVersion.Current);                   
                    db.Dispose();
                                 
            }         
        }
    }
}

//db.RetainOriginalThumbnailBitmap = true; //post R2012               
//Document doc = (Document)db.AcadDatabase; //com object     


WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Need help making a lisp function for Thumbnailer
« Reply #3 on: June 12, 2014, 07:34:54 PM »
You need to physically open the document to get there.  The document holds the database.

Core console may be helpful to you here. http://through-the-interface.typepad.com/through_the_interface/2012/02/the-autocad-2013-core-console.html

BillZndl

  • Guest
Re: Need help making a lisp function for Thumbnailer
« Reply #4 on: June 16, 2014, 06:30:04 AM »
I'm using 2012 so no go there.
One comment in the links that Keith posted said that odbx and net vanquishes that thumbnail on saving so it's probably a dead end for me.
I don't have that many drawings to change so I just won't worry about the thumbnail.

Thanks for the help!

BillZndl

  • Guest
Re: Need help making a lisp function for Thumbnailer
« Reply #5 on: June 16, 2014, 04:53:42 PM »
Acad 2012 VS 2010 Ex.

Just thought I'd share what I learned today.

I rewrote my lisp odbx program to net using ReadDwgFile.
If I put 2 lines of code before the SaveAs, the bitmap is saved with the drawing and appears at the next open dialog.
Why? I don't know.
Code: [Select]
Bitmap thumb = database.ThumbnailBitmap;
database.ThumbnailBitmap = thumb;
database.SaveAs(str, DwgVersion.Current);

Here's the whole routine, saved as a CommandMethod
Code: [Select]
public void FindTextInDrawing()
        {
            Document document = AcadApp.DocumentManager.MdiActiveDocument;           
            Editor editor = document.Editor;           
           
            string filename = filePath + loginname + "lundfl.txt";                           //list of filenames.
            string OldNums = "G:\\AutoCAD2012Support\\LispText\\OldParts.txt";
            string NewNums = "G:\\AutoCAD2012Support\\LispText\\NewParts.txt";

            if (File.Exists(filename) && File.Exists(OldNums) && File.Exists(NewNums))
            {
                editor.WriteMessage("Please wait while files are being searched.");

                using (document.LockDocument())
                {                   
                      try
                        {
                            string[] lines = File.ReadAllLines(filename);   //array of filename strings.
                            string[] OldParts = File.ReadAllLines(OldNums);
                            string[] NewParts = File.ReadAllLines(NewNums);

                            using (Transaction transaction = document.TransactionManager.StartTransaction())
                            {
                                foreach (string str in lines)
                                {
                                    editor.WriteMessage("\nFileName: " + str);
                                    Database database = new Database(false, false);
                                    database.ReadDwgFile(str, System.IO.FileShare.ReadWrite, false, "");

                                    BlockTable table = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForWrite);
                                    BlockTableRecord btr = (BlockTableRecord)transaction.GetObject(table[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                                    foreach (ObjectId oid in btr)
                                    {                                       
                                        if (oid.ObjectClass.DxfName == "TEXT")
                                        {
                                            string op = "";
                                            string np = "";

                                            DBText dbt = (DBText)transaction.GetObject(oid, OpenMode.ForWrite);

                                            for (int i = 0; i < OldParts.Length; i++)
                                            {
                                                op = OldParts[i];
                                                np = NewParts[i];

                                               if (dbt.TextString.Contains(op))
                                                {
                                                   string val = dbt.TextString;
                                                   val = val.Replace(op, np);                                                   
                                                   dbt.TextString = val;
                                                   editor.WriteMessage("\nReplaced: " + op + " with " + np);
                                                }
                                            }
                                        }
                                    }

                                    Bitmap thumb = database.ThumbnailBitmap;
                                    database.ThumbnailBitmap = thumb;

                                    database.SaveAs(str, DwgVersion.Current);
               
                                    database.Dispose();
                                }
                                transaction.Commit();
                            }                           
                        }                                       //try block

                        catch (System.Exception exception)
                        {
                            editor.WriteMessage("\nError: " + exception);
                        }
                        finally
                        {
                            if (MessageBox.Show("Close the AutoCAD Application?", "Replace Text", MessageBoxButtons.YesNo) == DialogResult.Yes)
                            {
                                AcadApp.Quit();
                            }
                        }                   
                }                                              //doc lock.                         
            }                                                  //if string input ok.
            else
            {
                editor.WriteMessage("\nCheck that all files exist.\n");             
            }
         }     
« Last Edit: June 17, 2014, 06:42:24 AM by BillZndl »