.NET

Reply
Valued Contributor
swaywood
Posts: 91
Registered: ‎08-22-2013
Message 1 of 10 (391 Views)
Accepted Solution

i want to create a viewport in a layout, but the code generate a importdll error

391 Views, 9 Replies
03-07-2014 06:21 PM

i want to create a viewport in a layout, and i get some code from kean's blog, but the code generate a importdll error, please help me!

the system is Win7x64bit+AutoCAD2013

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcGi = Autodesk.AutoCAD.GraphicsInterface;

namespace sample
{
public class paperViewport
{

[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@@PBVAcDbViewport@@@Z")]
extern static private int acedSetCurrentVPort(IntPtr AcDbVport);

[CommandMethod("CreateFloatingViewport")]
public static void CreateFloatingViewport()
{
// Get the current document and database, and start a transaction
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Paper space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.PaperSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Switch to the previous Paper space layout
Application.SetSystemVariable("TILEMODE", 0);
acDoc.Editor.SwitchToPaperSpace();

// Create a Viewport
Viewport acVport = new Viewport();
acVport.CenterPoint = new Point3d(3.25, 3, 0);
acVport.Width = 6;
acVport.Height = 5;

// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acVport);
acTrans.AddNewlyCreatedDBObject(acVport, true);

// Change the view direction
acVport.ViewDirection = new Vector3d(1, 1, 1);

// Enable the viewport
acVport.On = true;

// Activate model space in the viewport
acDoc.Editor.SwitchToModelSpace();

// Set the new viewport current via an imported ObjectARX function

//error message:Unable to find an entry point named '?' in DLL 'acad.exe'.
acedSetCurrentVPort(acVport.UnmanagedObject);

// Save the new objects to the database
acTrans.Commit();
}
}

}

 

}

Need to use exported names from 2013 and that function moved from acad.exe to accore.dll in 2013

Link for getting exported names

 

http://www.theswamp.org/index.php?topic=41527.msg499429#msg499429

 

And when need to change values to ones in red below

 

[DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@@PEBVAcDbViewport@@@Z")]

      

Valued Mentor
jeff
Posts: 330
Registered: ‎05-12-2009
Message 2 of 10 (378 Views)

Re: i want to create a viewport in a layout, but the code generate a importdll e

03-07-2014 07:49 PM in reply to: swaywood

Need to use exported names from 2013 and that function moved from acad.exe to accore.dll in 2013

Link for getting exported names

 

http://www.theswamp.org/index.php?topic=41527.msg499429#msg499429

 

And when need to change values to ones in red below

 

[DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@@PEBVAcDbViewport@@@Z")]

      

You can also find your answers @ TheSwamp
Valued Contributor
swaywood
Posts: 91
Registered: ‎08-22-2013
Message 3 of 10 (365 Views)

Re: i want to create a viewport in a layout, but the code generate a importdll e

03-07-2014 10:00 PM in reply to: jeff
thanks a lot jeff. but how did you know this? and where are you learn from this knowlage point?
Valued Mentor
jeff
Posts: 330
Registered: ‎05-12-2009
Message 4 of 10 (358 Views)

Re: i want to create a viewport in a layout, but the code generate a importdll e

03-07-2014 10:24 PM in reply to: swaywood

Just google "name mangling" or "name decoration"

 

 

Also extern and __declspec for not being managled.

You can also find your answers @ TheSwamp
Valued Contributor
swaywood
Posts: 91
Registered: ‎08-22-2013
Message 5 of 10 (348 Views)

Re: i want to create a viewport in a layout, but the code generate a importdll e

03-07-2014 11:16 PM in reply to: jeff

i registed yesterday but still not approved by TheSwamp.

 

Valued Mentor
jeff
Posts: 330
Registered: ‎05-12-2009
Message 6 of 10 (344 Views)

Re: i want to create a viewport in a layout, but the code generate a importdll e

03-07-2014 11:20 PM in reply to: swaywood

Will send mods a message that swaywood is trying join.

 

Did you use swaywood as user name?

You can also find your answers @ TheSwamp
Valued Contributor
swaywood
Posts: 91
Registered: ‎08-22-2013
Message 7 of 10 (340 Views)

Re: i want to create a viewport in a layout, but the code generate a importdll e

03-07-2014 11:29 PM in reply to: swaywood
yes
it is swaywood
thanks
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 8 of 10 (334 Views)

Re: i want to create a viewport in a layout, but the code generate a importdll e

03-07-2014 11:43 PM in reply to: swaywood

I have no luck with this code snippet on my A2014

but the follwing code does the job

Command only works in paperspace!

   

        //	http://adndevblog.typepad.com/autocad/2012/08/create-paper-space-viewports.html
        [CommandMethod("VW")]
        public void tryCreateViewPort()
        {
            // Only works in paperspace
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                   // ed.SwitchToPaperSpace();
                    ObjectId mCurViewportId = ed.ActiveViewportId;

                    if (mCurViewportId == ObjectId.Null)
                    {
                        ed.WriteMessage("\nCommand only works in paperspace.");

                        return;

                    }

                    Viewport pCurViewport = tr.GetObject(mCurViewportId, OpenMode.ForRead) as Viewport;

                    if (pCurViewport == null)
                    {

                        ed.WriteMessage("\nCannot get active viewport.");

                        return;

                    }

                    if (pCurViewport.Number != 1)
                    {

                        ed.WriteMessage("\nCommand only works in paperspace.");

                        return;

                    }

                    // Ask for the position

                    Point3d pt1; Point3d pt2;

                    pt1 = ed.GetPoint("\nSelect first corner: ").Value;

                    pt2 = ed.GetCorner("\nSelect second corner: ", pt1).Value;

                    // Ask for the view to use

                    string mViewName;

                    mViewName = ed.GetString("\nEnter name of view to use: ").StringResult;

                    // Create new viewport

                    Viewport pViewport = new Viewport();

                    pViewport.Width = Math.Abs(pt2.X - pt1.X);

                    pViewport.Height = Math.Abs(pt2.Y - pt1.Y);

                    pViewport.CenterPoint = new Point3d(

                      pt1.X + (pt2.X - pt1.X) / 2,

                      pt1.Y + (pt2.Y - pt1.Y) / 2,

                      pt1.Z);

                    // Append new viewport to paper space

                    BlockTable pTable;

                    BlockTableRecord pPsBTR;

                    pTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    pPsBTR = (BlockTableRecord)tr.GetObject(pTable[BlockTableRecord.PaperSpace], OpenMode.ForWrite);

                    ObjectId mViewPortId = pPsBTR.AppendEntity(pViewport);

                    tr.AddNewlyCreatedDBObject(pViewport, true);
                    pViewport.On = true;
                    pViewport.FastZoomOn = true;

                    pViewport.SetUcs(pViewport.CenterPoint, Vector3d.XAxis, Vector3d.YAxis);


                    // Set the view



                    ViewTable pViewTable = (ViewTable)tr.GetObject(db.ViewTableId, OpenMode.ForRead);

                    // initialize the layout

                    // Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("TILEMODE", 0);
                    //  Layout layout = tr.GetObject(pPsBTR.LayoutId, OpenMode.ForWrite) as Layout;
                    //  LayoutManager.Current.CurrentLayout = "Layout2";
                    // layout.Initialize();

                    ViewTableRecord pViewTR;
                    if (pViewTable.Has(mViewName))
                    {
                        pViewTR = (ViewTableRecord)tr.GetObject(pViewTable[mViewName], OpenMode.ForWrite);
                    }
                    else
                    {
                        pViewTR = new ViewTableRecord();
                        pViewTR.Name = mViewName;
                        pViewTR.IsPaperspaceView = true;
                        pViewTable.UpgradeOpen();
                        ObjectId vtId = pViewTable.Add(pViewTR);

                        tr.AddNewlyCreatedDBObject(pViewTR, true);
                    }


                    ed.SetCurrentView(pViewTR);

                    tr.Commit();

                    Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("TILEMODE", 0);
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
            }
        }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 9 of 10 (326 Views)

Re: i want to create a viewport in a layout, but the code generate a importdll e

03-08-2014 12:30 AM in reply to: Hallex
Valued Contributor
swaywood
Posts: 91
Registered: ‎08-22-2013
Message 10 of 10 (306 Views)

Re: i want to create a viewport in a layout, but the code generate a importdll e

03-08-2014 05:17 AM in reply to: Hallex

Hi Hallex:

thanks a lot, u r so warmhearted.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.