.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Export the selection entity to wmf file ?

4 REPLIES 4
Reply
Message 1 of 5
yanasdf789
1083 Views, 4 Replies

Export the selection entity to wmf file ?

I try to export the wmf file  wilth  "Autodesk.AutoCAD.Interop.DLL",I find a trouble

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.PlottingServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Interop;
using System.Runtime.InteropServices;

 [CommandMethod("NewExport")]
        static public void NewExplort()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            AcadDocument acadDoc = doc.AcadDocument as AcadDocument;
            string exportFile = null;
            exportFile = "C:\\Temp\\Test1.wmf";
            SelectionSet s1=getVll();

            AcadSelectionSet sset=s1 as AcadSelectionSet;
            //***it is error  the sset  is null,but the s1 is not null
           
            acadDoc.Export(exportFile, "WMF", sset);

        }
       static public SelectionSet getVll()
        {
            SelectionSet SS = null;
            Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
           // Entity entity = null;
            DBObjectCollection entityCollection = new DBObjectCollection();
            PromptSelectionResult ents = ed.SelectAll();
            if (ents.Status == PromptStatus.OK)
            {
                using (Transaction transcation = db.TransactionManager.StartTransaction())
                {
                     SS = ents.Value;
                     transcation.Commit();
                    
                }



            }
            return SS;


        }

 AcadSelectionSet sset=s1 as AcadSelectionSet; // here  is error  ,the sset is null ,but the s1 is not null?how to modify?

4 REPLIES 4
Message 2 of 5
mzakiralam
in reply to: yanasdf789

you can try with below code:

 

ents.Value.GetObjectIds()

 

Message 3 of 5
yanasdf789
in reply to: mzakiralam

           [CommandMethod("NewExport")]
        static public void NewExplort()
        {

            Document doc = Application.DocumentManager.MdiActiveDocument;
            AcadDocument acadDoc = doc.AcadDocument as AcadDocument;
           
            string exportFile = null;
            exportFile = "C:\\Temp\\Test1.wmf";
         
            DBObjectCollection objectColletion = getVll();
            



            //acadDoc.SelectionSets.Item("sset").Delete();
            AcadSelectionSet sset = default(AcadSelectionSet);
           
            AcadSelectionSet ss = acadDoc.SelectionSets.Add("sset");
                      
          
           AcadSelectionSet mm = new AcadSelectionSetClass();
            for (int j = 0; j < objectColletion.Count; j++)
           {
              object bb = objectColletion[j] as object;
             ss.AddItems(bb);
              sset.AddItems(bb);


          }
            acadDoc.Export(exportFile, "WMF", ss);
            acadDoc.SelectionSets.Item("sset").Delete();

        }   





  static public DBObjectCollection getVll()
        {
            SelectionSet SS = null;
            Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            Entity entity = null;
            DBObjectCollection entityCollection = new DBObjectCollection();
            PromptSelectionResult ents = ed.SelectAll();
            if (ents.Status == PromptStatus.OK)
            {
                using (Transaction transcation = db.TransactionManager.StartTransaction())
                {
                    // SS = ents.Value;
                    SelectionSet sss = ents.Value;
                    foreach (ObjectId id in sss.GetObjectIds())
                    {
                        entity = transcation.GetObject(id, OpenMode.ForWrite, true) as Entity;
                        if (entity != null)
                            entityCollection.Add(entity);

                    }
                     transcation.Commit();                 
                }
            }
            return entityCollection;
        }

 it is also  error here!  "object bb = objectColletion[j] as object; ss.AddItems(bb);"?  can you help me? how to modify?

is it must be add  " Autodesk.AutoCAD.Interop"DLL  ?

Message 4 of 5
norman.yuan
in reply to: yanasdf789

As you already know that since you need to use COM API's AcadDocument.Export() to export WMF file, you need to build an AcadSelectionSet object.

 

After you create he AcadSelectionSet object, you have different ways to populate the AcadSelectionSet object.

 

1. Simply use the COM method AcadSelectionSet.Select()/Selectxxx(). It is very simple if you only need to select all entities.

2. If you like to write a lot of code and try .NET API way to select thinfs in AutoCAD, you can use .NET API's Editor.SelectXXX() method, as you did in your 2 posts. However, you must convert the selected objects in .NET API into correctc CPM API type in order to add them into AcadSelectionSet.

 

AcadSelectionSet.AddItems() takes an array of AcadEntity object, nothing else. which you did wrong in your 2 posted code snippets. In the first set of code, you tried to use Aitodesk.AutoCAD.EditorInput.SelectionSet as AcadSelectionSet. Naturally, they are not the same thing. In the second try, you tries to add and array of Autodesk.AutoCAD.DatabaseService.DBObject into AcadSelectionSet. Again, DBObject in .NET API is not the samething as COM API's AcadEntity.

 

Below is the code that worked (I used both COM API AcadSelectionSet.Select() and .NET API's Editor.SelectAll() to populate AcadSelectionSet, just to show how to do things in both way):

 

using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;

[assembly: CommandClass(typeof(ExportWmf.MyCommands))]

namespace ExportWmf
{
    public class MyCommands
    {
        private static string wmfFile=@"C:\Temp\MyDrawing";

        [CommandMethod("WmfExport")]
        public static void RunMyCommand()
        {
            Document dwg = Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            AcadSelectionSet ss = null;
            AcadDocument doc = (AcadDocument)dwg.AcadDocument;

            try
            {
                AcadApplication app=Application.AcadApplication as AcadApplication;
                app.ZoomExtents();

                //--Use COM API to select objects
                //ss = GetAcadSelectionSet1(doc);

                //Use .NET API to select objects, and 
                //selected objects must to be cast to AcadEntity
                //before populated into AcadSelecctionSet
                ss = GetAcadSelectionSet2(dwg);

                if (System.IO.File.Exists(wmfFile)) System.IO.File.Delete(wmfFile);

                doc.Export(wmfFile, "WMF", ss);
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nCommand \"MyCmd\" failed:");
                ed.WriteMessage("\n{0}\n{1}", ex.Message, ex.StackTrace);
            }
            finally
            {
                ss.Delete();
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }

        private static AcadSelectionSet GetAcadSelectionSet1(AcadDocument doc)
        {
            AcadSelectionSet ss = doc.SelectionSets.Add("WmfSet");

            ss.Select(AcSelect.acSelectionSetAll);

            return ss;
        }

        private static AcadSelectionSet GetAcadSelectionSet2(Document dwg)
        {
            AcadDocument doc = dwg.AcadDocument as AcadDocument;
            AcadSelectionSet ss = doc.SelectionSets.Add("WmfSet");

            List<AcadEntity> lst = new List<AcadEntity>();

            PromptSelectionResult res = dwg.Editor.SelectAll();
            if (res.Status == PromptStatus.OK)
            {
                using (Transaction tran = dwg.TransactionManager.StartTransaction())
                {
                    foreach (ObjectId id in res.Value.GetObjectIds())
                    {
                        Entity ent = (Entity)tran.GetObject(id, OpenMode.ForRead);
                        lst.Add(ent.AcadObject as AcadEntity);
                    }

                    tran.Commit();
                }

                ss.AddItems(lst.ToArray());
                return ss;
            }
            else
            {
                return null;
            }
        }
    }
}

 HTH

 

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 5
yanasdf789
in reply to: norman.yuan

Hi norman.yuan ,
Thank you ! I get it,I love U

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost