I'm learning a lot on my first dot net routine thanks to the group.
I'm looking for help with the form1 hide or unload and the form1 show.
The sample routine works when run as a module but I get a elockViolation when run from a button.
basically, if I select the button click the form hides like it should and I can pick the two pioints to draw the lines and polylines buy I get the
elockViolation and the routine does not draw the lines and polylines.
Any direction, comments or links are welcome.
thank you for your help.
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic
Public Class Class1
<CommandMethod("thres2")> _
Public Sub thresholdloadform()
Dim Form1 As New Form1
'Form1.Visible = True
Form1.Show()
End Sub
End Class
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
'' Get the current database and start the Transaction Manager
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Dim pPtRes As PromptPointResult
Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
Dim x1, y1, dist1 As Double
Dim Point101 As New Point3d
Dim Point102 As New Point3d
Dim Point103 As New Point3d
Dim Point104 As New Point3d
Dim Point105 As New Point3d
Dim Point106 As New Point3d
Dim Point107 As New Point3d
Dim Point108 As New Point3d
Dim Point109 As New Point3d
Dim Point110 As New Point3d
Dim dblRot As Double
Dim pi As Double = Math.PI
'' Prompt for the start point
pPtOpts.Message = vbLf & "Enter the start point: PT1 "
pPtRes = acDoc.Editor.GetPoint(pPtOpts)
Dim pt1 As Point3d = pPtRes.Value
'' Exit if the user presses ESC or cancels the command
If pPtRes.Status = PromptStatus.Cancel Then Exit Sub
'' Prompt for the end point
pPtOpts.Message = vbLf & "Pick Direction to PT2 "
pPtOpts.UseBasePoint = True
pPtOpts.BasePoint = pt1
pPtRes = acDoc.Editor.GetPoint(pPtOpts)
Dim pt2 As Point3d = pPtRes.Value
If pPtRes.Status = PromptStatus.Cancel Then Exit Sub
Dim point1 As New Point3d
Dim point2 As New Point3d
x1 = pt1(0) - pt2(0)
y1 = pt1(1) - pt2(1)
dist1 = Math.Sqrt(x1 ^ 2 + y1 ^ 2)
point1 = pt1
point2 = pt2
Dim pt10 As New Point2d(pt1.X, pt1.Y)
Dim pt20 As New Point2d(pt2.X, pt2.Y)
Application.ShowAlertDialog("Angle from XAxis: " & pt10.GetVectorTo(pt20).Angle.ToString())
dblRot = pt10.GetVectorTo(pt20).Angle.ToString()
'' Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
Dim acBlkTbl As BlockTable
Dim acBlkTblRec As BlockTableRecord
'' Open Model space for write
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _
OpenMode.ForRead)
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForWrite)
'Points for lines
Dim Point100 As Point3d = PolarPoint(pt1, (pi + dblRot), 0)
Dim Point100a As Point3d = PolarPoint(pt1, (pi + dblRot), 0)
Point101 = PolarPoint(Point100, (dblRot), 10) 'draws in direction of pick point pt2
Point102 = PolarPoint(Point100, (1.570796327 + dblRot), -100)
Point103 = PolarPoint(Point100, (1.570796327 + dblRot), 100)
Point104 = PolarPoint(Point101, (1.570796327 + dblRot), -100)
Point105 = PolarPoint(Point101, (1.570796327 + dblRot), 100)
'Points for polylines
Point106 = PolarPoint(Point100a, (dblRot), 10) 'draws in direction of pick point pt2
Point107 = PolarPoint(Point100a, (1.570796327 + dblRot), -100)
Point108 = PolarPoint(Point100a, (1.570796327 + dblRot), 100)
Point109 = PolarPoint(Point106, (1.570796327 + dblRot), -100)
Point110 = PolarPoint(Point106, (1.570796327 + dblRot), 100)
'' Defines the new lines
Dim acLine As Line = New Line(pt1, pt2)
Dim acLine0 As Line = New Line(pt1, Point100)
Dim acLine1 = New Line(Point102, Point103)
Dim acLine2 = New Line(Point103, Point105)
Dim acLine3 = New Line(Point105, Point104)
Dim acLine4 = New Line(Point104, Point102)
'' Add the line to the drawing
acBlkTblRec.AppendEntity(acLine)
acTrans.AddNewlyCreatedDBObject(acLine, True)
acLine.SetDatabaseDefaults()
'' Add the lines to the drawing
acBlkTblRec.AppendEntity(acLine1)
acTrans.AddNewlyCreatedDBObject(acLine1, True)
acLine1.SetDatabaseDefaults()
acBlkTblRec.AppendEntity(acLine2)
acTrans.AddNewlyCreatedDBObject(acLine2, True)
acLine2.SetDatabaseDefaults()
acBlkTblRec.AppendEntity(acLine3)
acTrans.AddNewlyCreatedDBObject(acLine3, True)
acLine3.SetDatabaseDefaults()
acBlkTblRec.AppendEntity(acLine4)
acTrans.AddNewlyCreatedDBObject(acLine4, True)
acLine4.SetDatabaseDefaults()
'' Defines the new polylines
Dim pline As Polyline = New Polyline()
pline.AddVertexAt(0, Convert2d(Point107), 0, 0, 0)
pline.AddVertexAt(1, Convert2d(Point108), 0, 0, 0)
pline.AddVertexAt(2, Convert2d(Point110), 0, 0, 0)
pline.AddVertexAt(3, Convert2d(Point109), 0, 0, 0)
pline.Closed = True
Dim objId As ObjectId = acBlkTblRec.AppendEntity(pline)
acTrans.AddNewlyCreatedDBObject(pline, True)
'' Commit the changes and dispose of the transaction
acTrans.Commit()
End Using
Me.Show()
End Sub
Public Shared Function PolarPoint(ByVal basepoint As Point3d, ByVal angle As Double, ByVal distance As Double) As Point3d
Return New Point3d((basepoint.X _
+ (distance * Math.Cos(angle))), (basepoint.Y _
+ (distance * Math.Sin(angle))), basepoint.Z)
End Function
Private Function Convert2d(ByVal pt As Point3d) As Point2d
Return New Point2d(pt.X, pt.Y)
End Function