Author Topic: First test proj getting closer  (Read 4752 times)

0 Members and 1 Guest are viewing this topic.

jcoon

  • Newt
  • Posts: 157
First test proj getting closer
« on: November 29, 2010, 08:02:44 AM »
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










Jeff H

  • Needs a day job
  • Posts: 6150
Re: First test proj getting closer
« Reply #1 on: November 29, 2010, 09:18:52 AM »
Lock the document



Code: [Select]

 Document doc = Application.DocumentManager.MdiActiveDocument;

            using (DocumentLock docLoc = doc.LockDocument())
            {
                ////Code Here

            }



VB

Code: [Select]

Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Using docLoc As DocumentLock = doc.LockDocument
            ''Code Here
        End Using

« Last Edit: November 29, 2010, 09:23:27 AM by Jeff H »

jcoon

  • Newt
  • Posts: 157
Re: First test proj getting closer
« Reply #2 on: November 29, 2010, 02:33:59 PM »
Jeff,

I need to lock the document just to hide and show a form? 

Thank you for your help.
John 

Alexander Rivilis

  • Bull Frog
  • Posts: 214
  • Programmer from Kyiv (Ukraine)
Re: First test proj getting closer
« Reply #3 on: November 29, 2010, 03:06:40 PM »
I need to lock the document just to hide and show a form? 
No. You have to lock the document in order to prevent exception elockViolation.
Put this code in sub Button1_Click before you change current document.

jcoon

  • Newt
  • Posts: 157
Re: First test proj getting closer
« Reply #4 on: November 29, 2010, 06:38:49 PM »
Alexander,

 
place the code Jeff provided?  what code do I need to enter at the "code here" statement?

Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Using docLoc As DocumentLock = doc.LockDocument
            ''Code Here
        End Using

do you have link that describes or has samples that show this lock and unlock feature.

Thank you for your help
John


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: First test proj getting closer
« Reply #5 on: November 29, 2010, 06:44:56 PM »
...
do you have link that describes or has samples that show this lock and unlock feature.



AutoCAD .NET Developer's Guide
See:
Lock and Unlock a Document
Copy Objects between Databases
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.

jcoon

  • Newt
  • Posts: 157
Re: First test proj getting closer
« Reply #6 on: November 30, 2010, 09:35:52 AM »
Kerry,

I'll take a look,  Hopefully I learn how the lock feature works.

Again thank you all for your help! I appreciate all the help I can get as I try to get my first dot net sample
working so I can finally get started with the move from vba to dot net.

John

Jeff H

  • Needs a day job
  • Posts: 6150
Re: First test proj getting closer
« Reply #7 on: November 30, 2010, 09:53:17 AM »
Look at these including execution context

jcoon

  • Newt
  • Posts: 157
Re: First test proj getting closer
« Reply #8 on: November 30, 2010, 05:15:01 PM »
Jeff,

Thanks !!!!!! that will help a lot

John