.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Drawing a line in model space

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
edube14
904 Views, 8 Replies

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 ?

 

Autocad Code

 

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
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

 

Thanks in advance !

 

8 REPLIES 8
Message 2 of 9
edube14
in reply to: edube14

The only thing I changed it's I remove the first line ?

 

<CommandMethod("AddLine")> _

Message 3 of 9
joelmckeown
in reply to: edube14

The <CommandMethod("AddLine")> _ line is needed because it allows your method to run when the command "AddLine" is entered in Autocad. Unless the method is being called from somewhere else in your code that is running off a command then it won't ever be executed unless you keep that line in.

 

Add that line back in, load your program in autocad and enter "AddLine" in the command line. The line will be drawn. 

 

Hope that helps.

Message 4 of 9
edube14
in reply to: joelmckeown

"Unless the method is being called from somewhere else in your code that is running off..."

It's what want ! I dont know why it does'nt work without this line ???

Message 5 of 9
dgorsman
in reply to: edube14

Thats the sort of thing that we need to know in order to properly help, otherwise we may lead you down the wrong path based on our individual assumptions.  Best to lay everything out - what doesn't work (doesn't load?  crashes at some point?), where does it fail (have you stepped through the code?), and so on.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 6 of 9
edube14
in reply to: dgorsman

I put a message at the end of the sub...

I see the message but not the line... no crash...

Message 7 of 9
edube14
in reply to: edube14

Here is the 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

 

Message 8 of 9
edube14
in reply to: edube14

No idea ?

Message 9 of 9
edube14
in reply to: edube14

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 !

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost