Author Topic: Create and save blank drawing  (Read 2104 times)

0 Members and 1 Guest are viewing this topic.

TJK44

  • Guest
Create and save blank drawing
« on: November 16, 2011, 03:58:38 PM »
Can anyone help me out with code on how to create a new drawing and save it?

Thanks for any help,

Ted

TJK44

  • Guest
Re: Create and save blank drawing
« Reply #1 on: November 16, 2011, 04:18:12 PM »
I tried just using this but didnt work and threw an error.

Code: [Select]
Dim db As Database = New Database(False, False)
            Using trans As Transaction = db.TransactionManager.StartTransaction()
                db.SaveAs("C:\test\DIDTHISWORK.dwg", DwgVersion.Newest)
            End Using

TJK44

  • Guest
Re: Create and save blank drawing
« Reply #2 on: November 16, 2011, 04:23:32 PM »
I solved how to get it to work. I changed the database line

Code: [Select]
            Dim db As Database = New Database(True, False)

BillZndl

  • Guest
Re: Create and save blank drawing
« Reply #3 on: November 16, 2011, 04:26:11 PM »
Have tried some searches for adding a new document to the document collection?

Here's one link:
http://www.theswamp.org/index.php?topic=39914.msg452117#msg452117

There are plenty of examples on SaveAs if you wish to save the document to a file name.


TJK44

  • Guest
Re: Create and save blank drawing
« Reply #4 on: November 16, 2011, 04:32:16 PM »
I wanted a new dwg file to be created in the background so I could then add xrefs to it without the user having to create the empty dwg. It's working now.

BillZndl

  • Guest
Re: Create and save blank drawing
« Reply #5 on: November 16, 2011, 04:34:04 PM »
I wanted a new dwg file to be created in the background so I could then add xrefs to it without the user having to create the empty dwg. It's working now.

Oh, you should have said that in the first place.
People can help you better if they know what you trying to do.
Glad you got it going.



Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Create and save blank drawing
« Reply #6 on: November 16, 2011, 04:36:21 PM »
< ... > It's working now.

Care to post your solution so others may learn from your testing ?


[< .. >
Oh, you should have said that in the first place.
People can help you better if they know what you trying to do.
Glad you got it going.

Can't argue with that.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TJK44

  • Guest
Re: Create and save blank drawing
« Reply #7 on: November 16, 2011, 04:40:22 PM »
Code: [Select]
            Dim xrefdb As Database = New Database(False, False)
            Dim fName2 As String = database.Filename
            Dim curdir2 As String = Path.GetDirectoryName(fName2)
            Dim db As Database = New Database(True, False)

            Using trans As Transaction = db.TransactionManager.StartTransaction()
                db.SaveAs(curdir2 & "\Master.dwg", DwgVersion.Newest)
            End Using

            xrefdb.ReadDwgFile(curdir2 & "\Master.dwg", FileShare.ReadWrite, False, "")

            Using trx As Transaction = xrefdb.TransactionManager.StartTransaction()

                Dim xrefBt As BlockTable = trx.GetObject(xrefdb.BlockTableId, OpenMode.ForWrite)

                Dim btrMs As BlockTableRecord = trx.GetObject(xrefBt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

                Dim files As String() = Directory.GetFiles(curdir2 & "\parts")

                'list the names of all files in the specified directory
                For Each file In files

                    Dim xrefObjId As ObjectId = xrefdb.AttachXref(file, Path.GetFileNameWithoutExtension(file))

                    Dim bref As New BlockReference(Point3d.Origin, xrefObjId)

                    btrMs.AppendEntity(bref)

                    Dim blname As String = "XRef - " & Path.GetFileNameWithoutExtension(file)

                    'Get the layer table first...
                    Dim lt As LayerTable = trx.GetObject(xrefdb.LayerTableId, OpenMode.ForRead)
                    Dim layerId As ObjectId = ObjectId.Null
                    'Check if EmployeeLayer exists...
                    If lt.Has(blname) Then
                        layerId = lt.Item(blname)
                    Else
                        'If not, create the layer here.
                        lt.UpgradeOpen()
                        Dim ltr As LayerTableRecord = New LayerTableRecord()
                        ltr.Name = blname ' Set the layer name
                        ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 7)
                        layerId = lt.Add(ltr)
                        trx.AddNewlyCreatedDBObject(ltr, True)
                    End If

                    bref.LayerId = layerId
                    trx.AddNewlyCreatedDBObject(bref, True)


                Next

                trx.Commit()

            End Using
            xrefdb.SaveAs(curdir2 & "\Master.dwg", DwgVersion.Newest)

I should have a try block in here too, didn't get around to that yet.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Create and save blank drawing
« Reply #8 on: November 16, 2011, 04:43:31 PM »
TJK44

How are you declaring this code snip.

ie
What is the method called.
What is the CommandMethod used
How about giving us enough so that we don't have to guess how you are using this snippet ... and don't need to spend time adding to the code so we can test it.

Are you calling it from the Command line or from a Modal or Modeless or Pallete dialog
« Last Edit: November 16, 2011, 04:48:59 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TJK44

  • Guest
Re: Create and save blank drawing
« Reply #9 on: November 16, 2011, 04:55:12 PM »
Sorry Kerry. Its at the end of a sub procedure, you want me to post the entire sub? What it does is it takes blocks from a drawing one by one if they have a certain attribute, creates a folder called parts, then drops each block into the folder as a part. Then creates the blank drawing and drops all the parts in as xrefs and also creates a separate layer for each xref. The part I posted essentially just pulls dwgs out of a folder and then xrefs them to the new dwg that is being created in the code.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Create and save blank drawing
« Reply #10 on: November 16, 2011, 05:26:52 PM »
Seems like a lot to put into one Sub.

anyway,
I personally don't need to see all the code ... but I do expect to be given all the information relating to a problem I'm asked to look at ; and enough code to demonstrate the issue.

You project sounds interesting. From the last message you posted it seems that all issues are resolved ... which is great.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TJK44

  • Guest
Re: Create and save blank drawing
« Reply #11 on: November 16, 2011, 05:36:19 PM »
If you need anymore information on it let me know, I'll be more than happy to help.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Create and save blank drawing
« Reply #12 on: November 16, 2011, 05:38:24 PM »

:) Thanks.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TJK44

  • Guest
Re: Create and save blank drawing
« Reply #13 on: November 16, 2011, 05:40:18 PM »
I did actually have the entire layer creation process in a function, but it wasn't working for me. Kept getting an error saying WrongDb I think. So I put it in the sub just to get it to work for the time being. Probably look into that tomorrow.