Hello!
What I need is: inside Autocad, the user should be able to type in a command and Windows Form should show asking for relevant information that will draw lines and text on the current drawing or a new one depending on user's choice.
I have a Visual Studio project with the 3.5 framework, Autocad plug-in template and its corresponding "myCommands.cs" file. I added a Windows Form and customized it so the user can input data for the creation of the lines to be drawn.
This is a representation of what I have on "myCommands.cs"
using System; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.EditorInput; [assembly: CommandClass(typeof(Test_010.MyCommands))] namespace Test_010 { public class MyCommands { [CommandMethod("ElCommando")] public void ElCommando() { myFormo newFormo = myFormo();
And for the click event of a certain (the only) button in the form I have this
private void btnCreate_Click(object sender, EventArgs e) {
// The code below works to draw lines, polylines, etc in the current active document, cannot create a new one and make it the active one
//Saving info in a custom struct named myStruct in an attempt to return the struct to the //ElCommando //but no clue how that would work since Application.ShowModalDialog(newFormo); is used //instead of newFormo.Show() and even then I would not know how or where to change the //return type for the Show() event
//Cannot create a new autocad database here, I get a "File Error" COM Exception
//of course using using Autodesk.AutoCAD.ApplicationServices;
//Document NewDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Add(@"acadiso.dwt");
//that's where I get the error
//Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = NewDoc;
//I'd like to use that line too when the user chooses to draw in a new document myStruct newStruct; newStruct.x1 = double.Parse(textBox1.Text); newStruct.y1 = double.Parse(textBox2.Text); newStruct.x2 = double.Parse(textBox3.Text); newStruct.y2 = double.Parse(textBox4.Text); using (Transaction acTrans = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction()) {
// Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead) as BlockTable; // Open the Block table record Model space for write BlockTableRecord acBlkTblRec; acBlkTblRec = (BlockTableRecord)acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite); //Lines Line zero = new Line(new Point3d(newStruct.x1, newStruct.y1, 0), new Point3d(newStruct.x2, newStruct.y2, 0)); zero.SetDatabaseDefaults(); //zero.Linetype = Autodesk.AutoCAD.GraphicsSystem.LinePattern.Dashed.ToString();<---- Also, Can't explicitly change from that type to string? An equivalent method? zero.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(0, 0, 252); // Add the new object to the block table record and the transaction acBlkTblRec.AppendEntity(zero); acTrans.AddNewlyCreatedDBObject(zero, true); // Save the new object to the database acTrans.Commit(); } this.Close(); }
What I would like would be for the ElCommando to show the form and to somehow gather data from the modal form and then whether the user picked created new drawing, draw the objects in the current drawing or a new drawing.
Thanks in advance for your help.
In another note. How do I get to draw a dynamic number of lines?
I'm trying to send data back to the command function (IMPORTANT) and create a new drawing file and make it the active document for AutoCAD (Not necessary if first case is solved).
CommandFlag.Session did not work =/
I also tried the other longer way of creating a new document in the Windows Form click event code but it doesn't work either.
string strTemplatePath = "acadiso.dwt";
DocumentCollection acDocMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
Document acDoc = acDocMgr.Add(strTemplatePath);
acDocMgr.MdiActiveDocument = acDoc;
=/
If you use CommandFlags.Session you have to use LockDocument,
see an explanation about this here:
http://adndevblog.typepad.com/autocad/2012/05/when-to-lock-the-document.html
Try this code snip on your form
private void btnCreate_Click(object sender, EventArgs e) { string filename = @"c:\Test\newdoc.dwg"; double x1; double x2; double y1; double y2; if (!(double.TryParse(textBox1.Text, out x1) && double.TryParse(textBox2.Text, out y1) && double.TryParse(textBox3.Text, out x2) && double.TryParse(textBox4.Text, out y2))) { MessageBox.Show("One or more parameters doesn't entered!"); return; } try { DocumentCollection docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager as DocumentCollection; Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; string temppath = "acadiso.dwt"; string fullpath = HostApplicationServices.Current.FindFile(temppath, doc.Database, FindFileHint.Default); using (DocumentLock doclock = doc.LockDocument()) { using (Database newdb = new Database(false, true)) { newdb.ReadDwgFile(fullpath, System.IO.FileShare.ReadWrite,false, ""); using (Transaction newtr = newdb.TransactionManager.StartTransaction()) { ObjectId mSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(newdb); BlockTableRecord newbtr = (BlockTableRecord)newtr.GetObject(mSpaceId, OpenMode.ForWrite); Line line = new Line(new Point3d(x1, y1, 0), new Point3d(x2, y2, 0)); newbtr.AppendEntity(line); newtr.AddNewlyCreatedDBObject(line, true); newtr.Commit(); newdb.SaveAs(filename, false,DwgVersion.Current,newdb.SecurityParameters); } } } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message); } this.Close(); }
Hi,
Maybe I miss read but why not just pass "this" to your newFormo .... "myFormo newFormo = myFormo(this);"
r,
dennis