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

How to increase C# AddIn speed..

9 REPLIES 9
Reply
Message 1 of 10
MarcoBragotto7559
858 Views, 9 Replies

How to increase C# AddIn speed..

Hi guys, I've a big big problem.  I need to draw, using my C# AddIn, 550000 3DFace at least, and it takes 331seconds (5.5minutes). How can I speed this process?

 

This is the code I use

           

foreach (var obj in objects)

{

   double[] p1 = new double[3];               

   double[] p2 = new double[3];               

   double[] p3 = new double[3];

               

   Point3d p = obj.Geometry.PointN(1).GetPoint3d();

       p1[0] = p.X; p1[1] = p.Y; p1[2] = p.Z;

       p = obj.Geometry.PointN(2).GetPoint3d();

       p2[0] = p.X; p2[1] = p.Y; p2[2] = p.Z;

       p = obj.Geometry.PointN(3).GetPoint3d();

       p3[0] = p.X; p3[1] = p.Y; p3[2] = p.Z;

               

   this.AcadDocument.ModelSpace.Add3DFace(p1, p2, p3, p1);

       p1 = null;

   p2 = null;

   p3 = null;

}

 

where GetPoint3d() method is

 

public static Point3d GetPoint3d(this DbGeometry g)

{

   return new Point3d(g.X.Value, g.Y.Value, g.Z.Value);

}

 

Any ides? Thanks a lot

Enrico

9 REPLIES 9
Message 2 of 10

Try using pure .NET and not using COM. Then compare speed...

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 3 of 10

My first suggestion would be, don't use COM.  The managed API generally runs much faster than COM, even though it may take a few extra lines of code to get the same thing done.  Particularly the lines of code which will deal with the transaction(s).

 

Working under the assumption that you are unfamiliar with the Managed API's, I have worked up a function you can try.

 

public void CreateFace(Point3d p1, Point3d p2, Point3d p3, Database db = null)
{
    if (db == null)
        db = HostApplicationServices.WorkingDatabase;
    using (Transaction Trans = db.TransactionManager.StartTransaction) {
        try {
            using (BlockTableRecord MS = Trans.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite, false, true)) {
                using (Face newFace = new Face(p1, p2, p3, true, true, true, true)) {
                    MS.AppendEntity(newFace);
                        Trans.AddNewlyCreatedDBObject(newFace, true);
                         }
			}
                        Trans.Commit();
        } catch (System.Exception ex) {
        Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\\nError Creating Face\\n" + ex.Message);
      }
   }
}

 You can try just calling this function instead of Add3dFace, and obviously while doing that, you'll want to skip creating the Double Arrays, and pass the Point3d's directly.

 

If that doesn't show a significant improvement in speed, then you might also try modifying this function and putting your foreach loop inside the using statement for the ModelSpace.  That would mean start your foreach on the line after using (BlockTableRecord MS = .....

 

and put the entire other using statement inside your foreach (using (Face newFace = ...

 

You'll need to have references to AcMgd.dll and AcDbMgd.dll in your project, and you'll need to import (using in c#)Autodesk.AutoCAD.DatabaseServices at least, maybe Autodesk.AutoCAD.ApplicationServices also.

 

One last thing, you didn't make it clear, (you did say addin) but this code will only work in a class library that is netloaded into AutoCAD, it will not work from a standalone .EXE.

Dave O.                                                                  Sig-Logos32.png
Message 4 of 10

In addition - looking beyond your code - is your geometry such that you could use a small number of meshes (ideally SubDMeshes) instead of 550,000 3dFaces? If you can use a mesh, you should find that the DWG will consume significantly less memory and should also update faster when orbitting.

Cheers,

Stephen Preston
Autodesk Developer Network
Message 5 of 10

FYI, as I read Stephens post, and also glanced at my own, I noticed something I think might be wrong.  I work in VB, so the Sub I posted was written in VB and converted on the developer fusion website, but when I wrote the error message line for the catch, I put "\n" in for newline characters instead of using VbCrLf because I knew I was going to convert the code to C# (you can't use \n in VB).

 

Anyway, the converted code came out with two slashes, and I think that is wrong.  It should only have one slash.  If I am wrong, then hopefully someone out there who codes in C# will correct me, either way, you should be able to get it right, knowing that there may be an issue, or not. 

Dave O.                                                                  Sig-Logos32.png
Message 6 of 10

Thanks again for the tip, Stephen, but in terms of my own purposes, I would have one question.  I think I already know the answer, but just in case I am wrong, and also for the benefit of others who may stumble on to this post later, would the meshes (SubDMeshes or otherwise) provide the benefit of 3D Clash Detection, or, as my past experience has indicated, would they fail to register as interferences?

Dave O.                                                                  Sig-Logos32.png
Message 7 of 10

Hi guys, first of all I wanna thank you all for the helps!  My AddIn is a C# DLL loaded via NETLOAD, so it's not a COM AddIn.  I will try solutions suggested and, as soon as I can, I will tell you news.

 

Thanks again

Enrico

Message 8 of 10

FYI, regardless of the fact that your assembly is being Netloaded into AutoCAD, it is using the COM type libraries.  They are referenced from the COM tab in the Add Reference Dialog.  If you use anything under the Autodesk.AutoCAD.Interop or Autodesk.AutoCAD.Interop.Common, then you are using COM.

Dave O.                                                                  Sig-Logos32.png
Message 9 of 10
MHeimann
in reply to: chiefbraincloud

If you use MarcoBragotto7559's example, be sure to put the

using (Transaction Trans = db.TransactionManager.StartTransaction)

and

Trans.Commit();

OUTSIDE of your loop. Starting and commiting changes takes a lot of time.

 

I would suggest a function like this:

 

public void CreateFace(Point3d p1, Point3d p2, Point3d p3, Database db, Transaction Trans)

*edit*: For maximum speed, I would even take the

using (BlockTableRecord MS = Trans.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite, false, true))

out of the function. Open it only once and pass it on as parameter.

Message 10 of 10

>>would the meshes (SubDMeshes or otherwise) provide the benefit of 3D Clash Detection, or, as my past experience has indicated, would they fail to register as interferences?

 

When you select a SubDMesh in the INTERFERE command, a task dialog is displayed iforming you that meshes are not supported by that command and giving you the option to convert the mesh to a smoothed or faceted solid or surface. A cool feature of the subdmesh is you can easily convert ot to a solid or surface once you've finished manipulating the mesh (or create one from a solid).

 

What approach you use depends on your applciation and the geometry you're working with. But if you're using a set of 3dfaces to represent a facetted surface, then its worth considering using subdmesh to do that and then converting the finished mesh ito a solid/surface if required.

 

Cheers,

Stephen Preston
Autodesk Developer Network

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