dwg open and close

dwg open and close

Anonymous
Not applicable
3,521 Views
14 Replies
Message 1 of 15

dwg open and close

Anonymous
Not applicable

I use AutoCAD plant3d 2016.

 

I want to open a new dwg during modeling work with .NET code

and after work (modeling etc), I want to close dwg that I opened with code.

 

Is there any good way? only .NET code.

0 Likes
3,522 Views
14 Replies
Replies (14)
Message 2 of 15

amitnkukanur
Collaborator
Collaborator

You want to do this task as window application or run inside autocad using netload

Senior Software Engineer
0 Likes
Message 3 of 15

Littlerubarb
Advocate
Advocate

Here is an example;

 

Public Sub dosomethingtodwg(mainDrawingFile As String)
Dim db As New Database(False, True)
db.ReadDwgFile(mainDrawingFile, System.IO.FileShare.ReadWrite, True, "")
Using db
Dim saveRequired As Boolean = False
db.dosomethingtodwg(True, False)
Using tr As Transaction = db.TransactionManager.StartTransaction()
dosomething.......
Next
tr.Commit()
tr.Dispose()
End Using
If saveRequired Then
db.SaveAs(mainDrawingFile, DwgVersion.Current)
End If
End Using

0 Likes
Message 4 of 15

ActivistInvestor
Mentor
Mentor

@Littlerubarb wrote:

Here is an example;

 

Public Sub dosomethingtodwg(mainDrawingFile As String)
Dim db As New Database(False, True)
db.ReadDwgFile(mainDrawingFile, System.IO.FileShare.ReadWrite, True, "")
Using db
Dim saveRequired As Boolean = False
db.dosomethingtodwg(True, False)
Using tr As Transaction = db.TransactionManager.StartTransaction()
dosomething.......
Next
tr.Commit()
tr.Dispose()
End Using
If saveRequired Then
db.SaveAs(mainDrawingFile, DwgVersion.Current)
End If
End Using  <-- closes mainDrawingFile


When you call Dispose() on a Database that was accessed via ReadDwgFile(), that closes the file and releases the lock on it, so that it can be accessed by other users.

 

Was that the question?

0 Likes
Message 5 of 15

Littlerubarb
Advocate
Advocate

Yes it does close the drawing! In an automated process you would want to close the file. If you have a better idea please post it!

0 Likes
Message 6 of 15

ActivistInvestor
Mentor
Mentor

@Littlerubarb wrote:

Yes it does close the drawing! In an automated process you would want to close the file. If you have a better idea please post it!


There is no better idea. The example code you show is closing the DWG file when it reaches the last End Using statement.  There's nothing else that needs to be done.

0 Likes
Message 7 of 15

Anonymous
Not applicable

Thank you. reply my question.

 

Sorry, can you change this example to c #?

 

The question is wrong. I should have done it with C# instead of vb.NET

 

I'm sorry. I don't know vb.NET code.  T_T

 

 

0 Likes
Message 8 of 15

Anonymous
Not applicable

The question is wrong. I should have done it with C# instead of vb.NET

 

I'm sorry. I don't know vb.NET code.  T_T

 

 

Please. c# code instead of vb.net

 

Thank you.

0 Likes
Message 9 of 15

ActivistInvestor
Mentor
Mentor

@Anonymous wrote:

The question is wrong. I should have done it with C# instead of vb.NET

 

I'm sorry. I don't know vb.NET code.  T_T

 

 

Please. c# code instead of vb.net

 

Thank you.


visit www.telerick.com and use their translator to translate VB.NET to C# and back.

0 Likes
Message 10 of 15

dgorsman
Consultant
Consultant

The C# will be much the same.  I recommend you try to rewrite it yourself first. Smiley Happy

 

By the way, this could be opening a can of worms since Plant3D is "project based" with a host of other considerations.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 11 of 15

ActivistInvestor
Mentor
Mentor

Sorry, I gave you a bad link.

 

The page where you can convert VB.NET to C# is http://converter.telerik.com/

 

But...

 

If you're not able to convert that relatively-simple VB.NET code to C#, what else are you expecting to be able to do with the open Database ?

0 Likes
Message 12 of 15

Anonymous
Not applicable

Sorry. I've never used vb.net.

 

I have used only c++ and c# on visual studio. I've never used anything else.

 

So I can convert c++ to c#, or c# to c++. a little.

 

Thank you

0 Likes
Message 13 of 15

Anonymous
Not applicable

I convert .net to C# that this code.

 

 

Dim db As New Database(False, True)
db.ReadDwgFile(mainDrawingFile, System.IO.FileShare.ReadWrite, True, "")

In this code, An error occurs in ReadDwgFile method. so I changed bool value.

 

 

false, true -> error,

true, false -> error,

false, false -> error,

true, true -> no error.

 

so I used below code

 

Database db = new Database(true, true);
//If dwg file doesn't exist, It occurs error. so I used SaveAs method
db.SaveAs(newDwgName, DwgVersion.Current); db.ReadDwgFile(newDwgName, System.IO.FileShare.ReadWrite, true, "");

Document openDoc = Application.DocumentManager.Open(newDwgName,false);

 

By the way, I have a problem. 

 

In new dwg, drawing pipe is not work.

 

Always pop up alert dialog that dwg insert to current project.

 

Help me~~~.

 

0 Likes
Message 14 of 15

Norman_Yuan
Mentor
Mentor

You question is quite vague to get a useful respond/answer. The code suggested in earlier reply may or may not suit to your need. You'd better describe the process of your need in more details:

 

What does "open" and "close" mean? drawing file can be opened/closed as Document in/from AutoCAD editor, or can be "opened/closed" as side Database without being visible in AutoCAD. When opening in AutoCAD editor as Document, it can be opened in Document context (so, it would not become MdiActiveDocument), or opened in Application context (it becomes MdiDocument upon opening)...

 

Without knowing the situation of your business requirement, it is difficult to provide useful code/suggestion, but you DO NOT need to create an empty data base, save it to file, then open it as Document. In most cases, you can use DocumentCollectionExtension.Add()/Open() to OPEN a drawing in AutoCAD editor.

 

Also, since you use AutoCAD vertical product (Acad Plant), opening drawing as side Database may not allow you to reach the vertical product's specific data.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 15 of 15

Anonymous
Not applicable

Thank you for your reply.

 

I want to create and open new dwg file in current AutoCAD project with C# code.

then, modeling this dwg file that I created.

And modeling is done, close dwg file.

 

I don't know what should I do.

 

You said DocumentcollectionExtension's Add, Open method,

are these method useful in this topic?

 

0 Likes