Newing a drawing is missing default styles

Newing a drawing is missing default styles

soonhui
Advisor Advisor
268 Views
4 Replies
Message 1 of 5

Newing a drawing is missing default styles

soonhui
Advisor
Advisor

Here's my code to create a new drawing from .Net code

 

    [CommandMethod(nameof(CreateNewDrawing))]

    public void CreateNewDrawing()
    {
        var fileName = @"E:\Sample Project\February 2025\EmptyDrawing.dwg";
        using var db = new Database(true, true);
        db.SaveAs(fileName, DwgVersion.Current);
    }

 

I need to stress that the correct file, EmptyDrawing.dwg is created at the designated location. But when I open it, and compare it with a new drawing I create via UI, there are critical difference, in that the (Civil 3D) styles are missing.

 

Here are the two screenshot ( the first screenshot is for the drawing created via .Net API, the second screenshot is the drawing created via UI) that compare them:

 

CodeNew.pngUINew.png

 

I'm pretty sure I don't create the drawing properly via code, any command that I miss?

##########

Ngu Soon Hui

##########

I'm the Benevolent Dictator for Life for MiTS Software. Read more here


I also setup Civil WHIZ in order to share what I learnt about Civil 3D
0 Likes
Accepted solutions (2)
269 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant
Accepted solution

Hi,

new Database(true, true) constructor creates a new drawing with no template (as the _NEW command when choosing "Open with no template").

You should use new Database(false, false) and ReadDwgFile with the path of the template you want to start with.

 

[CommandMethod(nameof(CreateNewDrawing))]
public void CreateNewDrawing()
{
    string fileName = @"E:\Sample Project\February 2025\EmptyDrawing.dwg";
    string templateName = "SomeTemplate.dwt";
    using var db = new Database(false, false);
    db.ReadDwgFile(templateName, FileOpenMode.OpenForReadAndAllShare, false, null);
    db.SaveAs(fileName, DwgVersion.Current);
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 5

soonhui
Advisor
Advisor

@_gile , so what's the exact command that is equivalent to the New? 

 

I guess that New command is just the above coode, plus use the template file at %localappdata%\C3D 20xx\enu\Template\_Autodesk Civil 3D (Metric) NCS.dwt, so it's just equivalent to the templateName = "%localappdata%\C3D 20xx\enu\Template\_Autodesk Civil 3D (Metric) NCS.dwt", but I don't feel like to hardcode it this way, because I don't want to change my code when I upgrade my Civil 3D version. 

 

Is there a upgrade proof way of reading in the template file?

##########

Ngu Soon Hui

##########

I'm the Benevolent Dictator for Life for MiTS Software. Read more here


I also setup Civil WHIZ in order to share what I learnt about Civil 3D
0 Likes
Message 4 of 5

_gile
Consultant
Consultant
Accepted solution

@soonhui  a écrit :

@_gile , so what's the exact command that is equivalent to the New? 


If you're talking about the "QNEW" command, you can get the template path with:

string templatePath = HostApplicationServices.Current.GetEnvironmentVariable("QnewTemplate");


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 5

ActivistInvestor
Mentor
Mentor

In the UI a template file (. dwt) is usually used as the basis for a new drawing. So you don't create a new database, instead you open a DWT file and save it as a DWG file. 

0 Likes