See the top rated post in this thread. Click here

Page 1 of 6 12345 ... LastLast
Results 1 to 10 of 52

Thread: Drawing a line in model space

  1. #1
    100 Club
    Join Date
    2008-04
    Location
    St-Ferdinand, Québec
    Posts
    141
    Login to Give a bone
    0

    Default Drawing a line in model space

    Hi guys !

    I want to draw a line, I tried the Autocad code from help and it does'nt work...

    Is someone one could help me ?

    http://docs.autodesk.com/ACD/2013/EN...ber=d30e720593

    Code:
    Imports Autodesk.AutoCAD.Runtime
    Imports Autodesk.AutoCAD.ApplicationServices
    Imports Autodesk.AutoCAD.DatabaseServices
    Imports Autodesk.AutoCAD.Geometry
     
    <CommandMethod("AddLine")> _
    Public Sub AddLine()
      '' Get the current document and database
      Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
      Dim acCurDb As Database = acDoc.Database
     
      '' Start a transaction
      Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
     
          '' Open the Block table for read
          Dim acBlkTbl As BlockTable
          acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
     
          '' Open the Block table record Model space for write
          Dim acBlkTblRec As BlockTableRecord
          acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
                                          OpenMode.ForWrite)
     
          '' Create a line that starts at 5,5 and ends at 12,3
          Dim acLine As Line = New Line(New Point3d(5, 5, 0), _
                                        New Point3d(12, 3, 0))
     
          '' Add the new object to the block table record and the transaction
          acBlkTblRec.AppendEntity(acLine)
          acTrans.AddNewlyCreatedDBObject(acLine, True)
     
          '' Save the new object to the database
          acTrans.Commit()
      End Using
    End Sub
    The only thing I changed it's I remove the first line: <CommandMethod("AddLine")> _

    Because Im calling this sub from another sub...

    Thanks in advance !
    Last edited by Dubweisertm; 2014-12-05 at 06:44 PM.

  2. #2
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,536
    Login to Give a bone
    0

    Default Re: Drawing a line in model space

    Quote Originally Posted by Dubweisertm View Post
    Because Im calling this sub from another sub...
    Context is everything... Aside from presumably no line being created, how do you know that 'it doesn't work'?

    Can you post the code for the rest of your VS Project?

    What happens when you step through your code?

    Are there any Exceptions thrown?

    Have you tried the code as-is before attempting to call from your other Sub?

    What version are you compiling for?

    Environment?



    As may have surmised, more information is needed.

    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3620, Core i7-7700K 4.2GHz, 64GB RAM, Samsung 970 Pro M.2, 8GB NVIDIA Quadro P4000

  3. #3
    100 Club
    Join Date
    2008-04
    Location
    St-Ferdinand, Québec
    Posts
    141
    Login to Give a bone
    0

    Default Re: Drawing a line in model space

    Sorry for missing details !

    There is no error, I know that is does'nt work because I put a message and I see it, but not the line...

    If I put back the <CommandMethod("AddLine")> _ the line is drawn...

    I use MVS Express 2012 and Autocad 2015.

    Here is the code:

    Code:
    Imports System.Collections.Generic
    Imports Autodesk.AutoCAD.ApplicationServices
    Imports Autodesk.AutoCAD.DatabaseServices
    Imports Autodesk.AutoCAD.EditorInput
    Imports Autodesk.AutoCAD.Internal
    Imports Autodesk.AutoCAD.Runtime
    Imports Autodesk.AutoCAD.Geometry
    
    Namespace CRGagnonPlugin 'Declares the name of a namespace
    
        'Declares the name of a class
        Public Class myCommands
    
             
            ' Function EffectiveName: GET EFFECTIVE NAME OF DYNAMIC BLOCK
    
            Public Function EffectiveName(tr As Transaction, blkRef As BlockReference) As String
    
                Dim blkTr As BlockTableRecord = Nothing
    
                If (blkRef.IsDynamicBlock) Or (blkRef.Name.StartsWith("*U", StringComparison.InvariantCultureIgnoreCase)) Then
                    blkTr = TryCast(tr.GetObject(blkRef.DynamicBlockTableRecord, OpenMode.ForRead), BlockTableRecord)
                Else
                    blkTr = TryCast(tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)
                End If
    
                'Return Effective Name
                Return blkTr.Name
    
            End Function
    
            
            ' Function InsertionPoint: GET INSERTION POINT
            
            Public Function InsertionPoint() As Point3d
    
                'Get the current database and start the Transaction Manager
                Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
                Dim acCurDb As Database = acDoc.Database
    
                'Declare prompt
                Dim pPtRes As PromptPointResult
                Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
    
                'Prompt for insertion point
                pPtOpts.Message = vbLf & "Specify insertion point: "
                pPtRes = acDoc.Editor.GetPoint(pPtOpts)
                Dim ptStart As Point3d = pPtRes.Value
    
                'Exit if the user presses ESC or cancels the command
                If pPtRes.Status = PromptStatus.Cancel Then
                    'Return 0,0,0 when cancelled
                    ptStart = New Point3d(0, 0, 0)
                    Return ptStart
                    Exit Function
                End If
    
                'Return Insertion Point
                Return ptStart
    
            End Function
    
            
            ' SUB BuildTable: BUILD TABLE WITH DATAS
            
            Public Sub BuildTable(intTableRows As Integer)
    
                'Ask for insertion point
                InsertionPoint()
    
                'Draws actual line
                Dim acadApp As Object
                acadApp = Application.AcadApplication
    
                Dim startPoint(0 To 2) As Double
                Dim endPoint(0 To 2) As Double
    
                startPoint(0) = 0 : startPoint(1) = 0 : startPoint(2) = 0
                endPoint(0) = 500 : endPoint(1) = 500 : endPoint(2) = 0
    
                acadApp.ActiveDocument.ModelSpace.AddLine(startPoint, endPoint)
    
                MsgBox("The line is supposed to be drawn...")
    
            End Sub
    
            
            ' Sub WindowsList: WINDOWS LIST (Get quantity of each window types)
            
            'Declares Autocad command name
            <CommandMethod("wli")> _
            Public Sub WindowsList()
    
                'Declares variables and objects
                Dim blkname As String
                Dim doc As Document = Application.DocumentManager.MdiActiveDocument()
                Dim db As Database = doc.Database
                Dim ed As Editor = doc.Editor
    
                'Handle all possible errors...
                Try
    
                    'Beginning of a Using block
                    Using tr As Transaction = db.TransactionManager.StartTransaction
    
                        'Asks for Selection Set (use this for select all: res = ed.SelectAll(filt))
                        Dim res As PromptSelectionResult
                        Dim tvs(0) As TypedValue
                        tvs(0) = New TypedValue(0, "insert")
                        Dim filt As SelectionFilter = New SelectionFilter(tvs)
                        res = ed.GetSelection(filt)
    
                        'Checks Selection Set (not empty)
                        If res.Status <> PromptStatus.OK Then
                            Return
                        End If
    
                        'Creates Datatable to store temporary datas (8072.OD ou P81G+21)
                        Dim table As New System.Data.DataTable
    
                        'Creates Datatable Columns
                        With table
                            .Columns.Add("IDVALUE", GetType(String))
                            .Columns.Add("IDQTY", GetType(Integer))
                        End With
    
                        'Looping Selection Set
                        For Each sobj As SelectedObject In res.Value
    
                            'Declares objects
                            Dim obj As DBObject = DirectCast(tr.GetObject(sobj.ObjectId, OpenMode.ForRead, False), DBObject)
                            Dim blkRef As BlockReference = TryCast(obj, BlockReference)
    
                            'Checks BlockReference (not empty)
                            If blkRef IsNot Nothing Then
    
                                'Function: Get Effective Name
                                blkname = EffectiveName(tr, blkRef)
    
                                'Checks if its a Dynamic Block
                                If blkRef.IsDynamicBlock Then
    
                                    'Declares AttributeCollection
                                    Dim attCol As AttributeCollection = blkRef.AttributeCollection
    
                                    'Looping AttributeCollection
                                    For Each attId As ObjectId In attCol
    
                                        'Declares AttributeReference
                                        Dim att As AttributeReference = TryCast(tr.GetObject(attId, OpenMode.ForWrite, False), AttributeReference)
    
                                        'Declares search variable
                                        Dim booFound As Boolean = False
    
                                        'Checks Attributes Tag value (must be ID for windows)
                                        If att.Tag = "ID" Then
    
                                            'Checks Datatable (not empty)
                                            If table.Rows.Count > 0 Then
    
                                                'Looping rowa in Datatable
                                                For Each row As DataRow In table.Rows
                                                    'Checks Windows Type it's not existing in Datatable
                                                    If row.Item("IDVALUE") = att.TextString.ToString Then
                                                        'If existing: add 1 to quantity
                                                        row.Item("IDQTY") = row.Item("IDQTY") + 1
                                                        'Set search variable
                                                        booFound = True
                                                    End If
                                                Next row
    
                                                'If not existing: add window type and 1 to quantity
                                                If booFound = False Then
                                                    table.Rows.Add(att.TextString.ToString, 1)
                                                End If
    
                                            Else
                                                'If Datatable is empty: add window type and 1 to quantity
                                                table.Rows.Add(att.TextString.ToString, 1)
                                            End If
    
                                        End If
    
                                    Next
    
                                End If
    
                            End If
    
                        Next
    
                        'Build the table with datas
                        BuildTable(table.Rows.Count)
                        'BuildTable()
    
                        ''Display results in command line
                        'For Each row As DataRow In table.Rows
                        'ed.WriteMessage(vbLf + row.Item("IDVALUE").ToString + ": " + row.Item("IDQTY").ToString)
                        'Next row
                        'tr.Commit()
    
                    'End of a Using block
                    End Using
    
                    'Handle all possible errors...
                    Catch ex As Autodesk.AutoCAD.Runtime.Exception
                        Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog((ex.ToString() & vbLf) + ex.Message)
                    Finally
                        ed.WriteMessage(vbLf + "Command completed...")
    
                End Try
    
            End Sub
    
        End Class
    
    End Namespace
    Thanks !

  4. #4
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,536
    Login to Give a bone
    0

    Default Re: Drawing a line in model space

    I've only skimmed your code (I'm viewing this post on iPhone), but I didn't notice an overload for AddLine() that supports a startPoint and endPoint as parameters.

    Cheers



    [Edit] -

    Code:
    // AddLine overload
    public void AddLine(Point3d startPoint, Point3d endPoint)
    {
         // ...
    }
    Last edited by BlackBox; 2014-12-06 at 07:45 PM.
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3620, Core i7-7700K 4.2GHz, 64GB RAM, Samsung 970 Pro M.2, 8GB NVIDIA Quadro P4000

  5. #5
    100 Club
    Join Date
    2008-04
    Location
    St-Ferdinand, Québec
    Posts
    141
    Login to Give a bone
    0

    Default Re: Drawing a line in model space

    Hi BlackBox !

    Im not sure if I understand what you're talking about..."Overload" (Im french user)

    I dont need to pass these two variables as arguments...

  6. #6
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,536
    Login to Give a bone
    0

    Default Re: Drawing a line in model space

    Quote Originally Posted by Dubweisertm View Post
    Hi BlackBox !

    Im not sure if I understand what you're talking about..."Overload" (Im french user)

    I dont need to pass these two variables as arguments...
    In your code here, you call the AddLine() Method as a member of Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication.ActiveDocument.ModelSpace, and supply both startPoint, and endPoint as parameters:

    Quote Originally Posted by Dubweisertm View Post
    Code:
    ' ...
    
            ' SUB BuildTable: BUILD TABLE WITH DATAS
            
            Public Sub BuildTable(intTableRows As Integer)
    
                'Ask for insertion point
                InsertionPoint()
    
                'Draws actual line
                Dim acadApp As Object
                acadApp = Application.AcadApplication
    
                Dim startPoint(0 To 2) As Double
                Dim endPoint(0 To 2) As Double
    
                startPoint(0) = 0 : startPoint(1) = 0 : startPoint(2) = 0
                endPoint(0) = 500 : endPoint(1) = 500 : endPoint(2) = 0
    
                acadApp.ActiveDocument.ModelSpace.AddLine(startPoint, endPoint)
    
                MsgBox("The line is supposed to be drawn...")
    
            End Sub
    
    ' ...


    Additionally, your AddLine() Sub does not have an available Method Overload which accepts parameters... Instead, simply add a Method Overload that accepts the parameters by Type you require (I'd suggest Point3d in lieu of Double), and call that Method Overload directly using .NET API in lieu of COM.

    Method Overload example (C#):

    Code:
    // ...
    
            // Method Overload for AddLine() which accepts two Point3d arguments
            public void AddLine(Point3d startPoint, Point3d endPoint)
            {
                Database db = HostApplicationServices.WorkingDatabase;
    
                // start the transaction
                using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
                {
    
                    // open the block table for read
                     BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
    
                     // conditionally open Paperspace or Modelspace/Active PViewport
                     ObjectId spaceId = (1 == GetVarInt("CVPORT") ? 
                        SymbolUtilityServices.GetBlockPaperSpaceId(db) : 
                        SymbolUtilityServices.GetBlockModelSpaceId(db));
    
                     BlockTableRecord btr = 
                         (BlockTableRecord)tr.GetObject(spaceId, OpenMode.ForWrite);
    
                    // create a line that uses the supplied startPoint, and endPoint
                    Line oLine = new Line(startPoint, endPoint);
    
                    // add the new object to the block table record and the transaction
                    btr.AppendEntity(oLine);
                    tr.AddNewlyCreatedDBObject(oLine, true);
    
                    // save the new object to the database
                    tr.Commit();
                }
            }
            private int GetVarInt(string sysVar)
            {
                return System.Convert.ToInt32(Application.GetSystemVariable(sysVar));
            }
    
    // ...


    ... And here's how you'd call this Method Overload:

    Code:
    // ...
    
                Point3d startPoint = new Point3d(0, 0, 0);
                Point3d endPoint = new Point3d(500, 500, 0);
    
                AddLine(startPoint, endPoint);
    
    // ...
    Last edited by BlackBox; 2014-12-08 at 09:44 PM.
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3620, Core i7-7700K 4.2GHz, 64GB RAM, Samsung 970 Pro M.2, 8GB NVIDIA Quadro P4000

  7. #7
    100 Club
    Join Date
    2008-04
    Location
    St-Ferdinand, Québec
    Posts
    141
    Login to Give a bone
    0

    Default Re: Drawing a line in model space

    I resolved the problem !

    Im calling BuildTable() inside Try/Using Block...

    I put the BuildTable() immediately afert the "End Try" and now the line is drawn !

    Thanks BlackBox for your time, I appreciate... I check what you just explaine above...
    Last edited by Dubweisertm; 2014-12-08 at 07:43 PM.

  8. #8
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,536
    Login to Give a bone
    0

    Default Re: Drawing a line in model space

    Glad you got it sorted.

    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3620, Core i7-7700K 4.2GHz, 64GB RAM, Samsung 970 Pro M.2, 8GB NVIDIA Quadro P4000

  9. #9
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    5,720
    Login to Give a bone
    0

    Default Re: Drawing a line in model space

    First, you don't need to remove the command attribute. Just call the method by typing the method name AddLine() in your code.

    Second, you are confusing the AddLine method in your code with the AcadApplication.ActiveDocument.ModelSpace.AddLine() method. This is actually a method in the COM API, as accessed from Application.AcadApplication. The first example you posted is from the managed .NET API. I'm not sure why its not working. Try populating your startpoint, endpoint arrays with reals instead of integers. For example, startPoint(0) = 0.0 or startPoint(0) = 0#.
    C:> ED WORKING....

  10. #10
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,536
    Login to Give a bone
    0

    Default Re: Drawing a line in model space

    Quote Originally Posted by Ed Jobe View Post
    Second, you are confusing the AddLine method in your code with the AcadApplication.ActiveDocument.ModelSpace.AddLine() method. This is actually a method in the COM API, as accessed from Application.AcadApplication.
    Aha - Thanks for clarifying - The only real COM I use, was for UndoMarks prior to dynamics for legacy versions of my Apps.

    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3620, Core i7-7700K 4.2GHz, 64GB RAM, Samsung 970 Pro M.2, 8GB NVIDIA Quadro P4000

Page 1 of 6 12345 ... LastLast

Similar Threads

  1. Model Documentation - Allow editing of line weights in drawing views
    By Wish List System in forum AutoCAD Wish List
    Replies: 1
    Last Post: 2016-10-25, 04:37 PM
  2. 2006: drawing scale model space
    By caduser50R in forum ACA General
    Replies: 6
    Last Post: 2014-11-07, 06:15 PM
  3. Replies: 0
    Last Post: 2011-10-21, 06:44 PM
  4. Drawing not visible in model space
    By hardygiant in forum AutoCAD General
    Replies: 28
    Last Post: 2011-04-01, 12:35 AM
  5. Connot see hidden line in Model space
    By Jack Cheong in forum AutoCAD General
    Replies: 4
    Last Post: 2007-10-19, 02:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •