Hi All,
I have a palette that I am attempting to create for a user interface for a custom command. The command works as a standalone command from the command line once the assembly is loaded. If I run the function/subroutine from the usercontrol load event, the command works. My issue is if i put the command in a button click event on the palette (usercontrol) the function/sub fails claiming that the "network can not be created due to an internal error", this appears to be because something is write protected.
the general function of the command is create a pipe network.
basic code of the usercontrol load event and button click event.
I don't understand how the command can work during the loading of the usercontrol/palette, but not in a button click event on the palette/usercontrol.
any push in the right direction would be great.
Dim c3d As New c3dtools Private Sub UserControl1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load GetPartsList() c3d.sysin() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click c3d.sysin() End Sub
to make the issue a little more clear here is the code contained within the button event...
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 'GetBaseObjects() Dim odocument As CivilDocument Dim doccol As Autodesk.AutoCAD.ApplicationServices.DocumentCollection Dim doc As Autodesk.AutoCAD.ApplicationServices.Document Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager Dim ed As Editor odocument = CivilApplication.ActiveDocument docCol = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager doc = m_docCol.MdiActiveDocument tm = m_doc.Database.TransactionManager ed = m_doc.Editor Dim trans As Transaction = tm.StartTransaction Dim onetworkid As ObjectId Dim onetwork As Network Try onetworkid = Network.Create(odocument, "Pipe Network 01") 'Fails here... Catch ex As Autodesk.AutoCAD.Runtime.Exception MsgBox(ex.Message, MsgBoxStyle.Information) End Try Try onetwork = trans.GetObject(onetworkid, OpenMode.ForWrite) Catch Exit Sub End Try trans.Commit() 'c3d.sysin() End Sub
Since you are placing the code on a palette, which is always modeless, you must lock the document before making any changes to it from a palette. Not sure how to write it in VB, but this works in C#:
private void button1_Click(object sender, EventArgs e) { string ntwrkName = "Network 1"; using (Autodesk.AutoCAD.ApplicationServices.DocumentLock doclock = AcApp.DocumentManager.MdiActiveDocument.LockDocument()) { using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartOpenCloseTransaction()) { try { ObjectId ntwrkId = Network.Create(CivilApplication.ActiveDocument, ref ntwrkName); } catch { } tr.Commit(); } } }
Also, be sure to use a variable passed as ByRef for the network name. This needs to be done so C3D can auto assign a new name if the name provided exists.
Thanks Jeff, that worked great.
And yes the variable was passed as a Ref, however, for what I am doing the network would be deleted anyway, so Civil 3d doesn't need to increment it.
Thanks again.