Hey.
I wrote a simple program to insert points with random coordinates:
Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.ApplicationServices.Application Imports Autodesk.AutoCAD.Geometry Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.ApplicationServices.DocumentExtension Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim n As Integer n = Val(TextBox1.Text) Dim a As Random = New Random Dim b As Random = New Random Dim c As Random = New Random For i = 1 To n Step 1 Dim x As Double = a.Next(100) Dim y As Double = b.Next(100) Dim z As Double = c.Next(100) If x = y Or x = z Then Dim d As Random = New Random Dim f As Random = New Random y = d.Next(100) z = f.Next(100) ElseIf y = z Then Dim g As Random = New Random z = g.Next(100) End If Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument Dim acCurDb As Database = acDoc.Database Using acLckDoc As DocumentLock = acDoc.LockDocument() Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction() Dim acBlkTbl As BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) Dim acBlkTblRec As BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite) Using acPoint As DBPoint = New DBPoint(New Point3d((Val(x)), (Val(y)), (Val(z)))) acBlkTblRec.AppendEntity(acPoint) acTrans.AddNewlyCreatedDBObject(acPoint, True) End Using acCurDb.Pdmode = 0 acCurDb.Pdsize = 0 acTrans.Commit() End Using End Using 'MsgBox("x= " & x & vbNewLine & "y= " & y & vbNewLine & "z= " & z) Next i End Sub End Class
The problem is that the same numbers are drawn every time - the "If" did not help anything. When I add MsgBox separately for each coordinate, a different number is drawn. How can I "refresh" the program after random one number without creating a pop-up window?
Another problem I have with the window itself. As I wrote the application in visual basic in autocad, windowsform was always visible on the screen and after clicking the button I had a result at once. Now in vb .net, when I click the button, to see the effect I have to minimize the program.
Another thing is that when I have the application turned on in vb .net, after clicking the window draw of autocad, my application minimizes what was not in vb in autocad. How can I change it? I compared the properties of Form in vb in autocad and in vb .net, but it does not have all the properties, and the ones that are are the same. Please help me.
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Hi,
You should not Lock the document and start a new Transaction at each loop in the For statement.
You'd rather use a NumericUpDown control instead of a TextBox because it is simpler and safer to get an integer.
You do not need to intantiate 3 Random object, you can use the same for X, Y and Z.
About the Form behavior, if you want someone to be able to help you, you have to provide more details about how you show the Form.
Here's a simple working example.
private void button1_Click(object sender, System.EventArgs e) { var doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument; var db = doc.Database; var ed = doc.Editor; int n = (int)numericUpDown1.Value; var random = new Random(); using (doc.LockDocument()) using (var tr = db.TransactionManager.StartTransaction()) { var modelSpace = (BlockTableRecord)tr.GetObject( SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite); for (int i = 0; i < n; i++) { var point = new DBPoint(new Point3d(random.Next(100), random.Next(100), random.Next(100))); modelSpace.AppendEntity(point); tr.AddNewlyCreatedDBObject(point, true); } tr.Commit(); } }
Thank you for your help, but I write in vb .net, and the code is probably in C #? I guessed that var is dim, but I do not know how I can change this: "int n = (int) numericUpDown1.Value" on the code in vb.
As for the properties of the form, they are as follows:
And in vb in autocad, despite clicking outside of the Form, my application did not minimize itself, and all the actions performed (eg inserting a point) were visible immediately without minimizing my application.
VB equivalent for:
int n = (int) numericUpDown1.Value
should be:
Dim n As Integer = CInt(numericUpDown1.Value)
or:
Dim n As Integer = DirectCast(numericUpDown1.Value, Int32)
Anyway, you can use some converter (note C# works as VB when VB options Strict and Infer are On). And if you're new to .NET, learn C# instead of VB because you'll find more help and examples written in C#.
About your form behavior, you should describe (show the code) how you show/open your window (i.e. as a Modal dialog or as a Modeless dialog) and which method you're using (see this topic).
Can't find what you're looking for? Ask the community or share your knowledge.