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

Filtered objects into a single polyline

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
gulzar25
939 Views, 10 Replies

Filtered objects into a single polyline

Hi,

I am filtering for objects based on layer as shown below.The layer consists of lines and arcs.Is there anyway to convert all these entities into a polyline and get the object id of this single entity.Appreciate any ideas.

 

Dim acTypValArPath(0) AsTypedValue

 Dim sf As SelectionFilter = New SelectionFilter(New TypedValue() {New TypedValue(8, "FirstBias")})

 

Dim ss As SelectionSet = acEditor.SelectAll(sf).Value 

Dim idarray As ObjectId() = ss.GetObjectIds()

 

Dim id AsObjectId

For Each id In idarray

    Dim entity As = trLocal.GetObject(id, OpenMode.ForRead, True)

Next id

 

If (typeof entity is Arc) Then

......

End If

 

If (typeof entity is Line) Then

.......

End If

 

Thanks

Gulzar

 

10 REPLIES 10
Message 2 of 11
_gile
in reply to: gulzar25

Hi,

 

As I replied you in this other thread, if you want to hard-code the "join" option of the "PEDIT" command, you can get some inspiration from this reply. The MJOIN command uses the PolylineSegement and PolylneSegmentCollection classes defined in the GeometryExtensions assembly. The source code (C#) and compiled assemblies are downloadable from this reply.

 

But it seems to me that you're missing the AutoCAD and .NET basics.

So, you'd perhaps rather do some "scripting" with the "PEDIT" command.

Here's an example using a Tony Tanzillo's Editor.Command extension method which allow to pass managed types arguments to the command (as the LISP "command" function).

 

Imports System.Reflection
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime

Namespace gulzar25.JoinIntoPolyline

    ' credit to Tony Tanzillo
    ' http://www.theswamp.org/index.php?topic=43113.msg483306#msg483306
    Module MyEditorExtensions

        Dim runCommand As MethodInfo = _
            GetType(Editor).GetMethod("RunCommand", BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public)

        <System.Runtime.CompilerServices.Extension> _
        Public Function Command(ed As Editor, ParamArray args As Object()) As PromptStatus
            If Application.DocumentManager.IsApplicationContext Then
                Throw New InvalidOperationException("Invalid execution context for Command()")
            End If
            If ed.Document <> Application.DocumentManager.MdiActiveDocument Then
                Throw New InvalidOperationException("Document is not active")
            End If
            Return DirectCast(runCommand.Invoke(ed, New Object() {args}), PromptStatus)
        End Function

    End Module

    Public Class Commands

        <CommandMethod("MJOIN")> _
        Public Sub MultipleJoin()

            Dim acEditor As Editor = Application.DocumentManager.MdiActiveDocument.Editor

            Dim sf As SelectionFilter = New SelectionFilter(New TypedValue() {New TypedValue(8, "FirstBias")})
            Dim psr As PromptSelectionResult = acEditor.SelectAll(sf)
            If psr.Status <> PromptStatus.OK Then
                Return
            End If

            acEditor.Command("_.pedit", "_multiple", psr.Value, "", "_join", 0.0, "")

        End Sub

    End Class

End Namespace

 

IMO, scripting AutoCAD commands with .NET looks like hunting mosquitoes with a bazooka, this task would have been much more simply done with LISP:

 

(defun c:join (/ ss)
  (if (setq ss (ssget "_X" '((8 . "FirstBias"))))
    (command "_.pedit" "_multiple" ss "" "_join" 0.0 "")
  )
  (princ)
)

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 11
gulzar25
in reply to: _gile

Hi Gilles,
Thanks for the reply.Yes i have just started doing customization and i dont know anything.I tried using the above code but it is giving error as
"Command is not a member of 'Autodesk.AutoCAD.EditorInput.Editor'".What am i supposed to add more.

Thanks
Gulzar
Message 4 of 11
_gile
in reply to: gulzar25

Did you add the 'MyEditorExtensions' module, from the upper code, to your project?

It's where the Editor.Command() extension method is defined.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 11
gulzar25
in reply to: _gile

Hi Gilles,
Thankyou so much it is working now.Sorry to ask so many questions but once i run the dll i have to individually select all the lines which i want to convert to polyline.Correct me if i am wrong i thought it will pick all the lines from layer and convert to polyline.

Thanks so much for your help.
Gulzar
Message 6 of 11
_gile
in reply to: gulzar25

The code I posted does select all entities on the "FirstBias" layer 

acEditor.SelectAll(sf)

and only uses this selection in the PEDIT command call:

acEditor.Command("_.pedit", "_multiple", psr.Value, "", "_join", 0.0, "")

The empty string ("") argument after psr.Value validates this selection.

 

 

The code would be safer if it filtered the entity types too:

Dim typedValues() As TypedValue = {New TypedValue(8, "FirstBias"), New TypedValue(0, "ARC,LINE,LWPOLYLINE")}
Dim sf As SelectionFilter = New SelectionFilter(typedValues)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 11
gulzar25
in reply to: _gile

Hi Giles,
After changing the code the below four options are popping in autocad if i click on cancel polyline is not created.
Convert Lines, Arcs and Splines to polylines [Yes/No]? <Y> y
Enter an option [Close/Open/Join/Width/Fit/Spline/Decurve/Ltype:Join
Enter fuzz distance or [Jointype] <0.0000>:0.00
Enter an option [Close/Open/Join/Width/Fit/Spline/Decurve/Ltype
gen/Reverse/Undo]: *Cancel*

Thanks
Gulzar
Message 8 of 11
_gile
in reply to: gulzar25

This is due to the PEDITACCEPT system variable setting.

 

Here's a (safer) new version which stores the current PEDITACCEPT value, changes it to 1 while running the PEDIT command and restore it to the previous value.

 

        <CommandMethod("MJOIN")> _
        Public Sub MultipleJoin()

            Dim acEditor As Editor = Application.DocumentManager.MdiActiveDocument.Editor
            Dim peditAccept As Short = CShort(Application.GetSystemVariable("PEDITACCEPT"))

            Try
                Application.SetSystemVariable("PEDITACCEPT", 1)
                Dim typedValues() As TypedValue = {New TypedValue(8, "FirstBias"), New TypedValue(0, "ARC,LINE,LWPOLYLINE")}
                Dim sf As SelectionFilter = New SelectionFilter(typedValues)
                Dim psr As PromptSelectionResult = acEditor.SelectAll(sf)
                If psr.Status <> PromptStatus.OK Then
                    Return
                End If

                acEditor.Command("_.pedit", "_multiple", psr.Value, "", "_join", 0.0, "")

            Catch ex As Exception
                MsgBox(ex.Message)

            Finally
                Application.SetSystemVariable("PEDITACCEPT", peditAccept)

            End Try

        End Sub

 

Assuming you're begining with AutoCAD customization, I think you didn't choose the easiest route with .NET. AutoLISP is really much simpler and you'd find much more help for these kind of basics.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 11
gulzar25
in reply to: _gile

Hi Gilles,
Yes i shouldnt have started in .net...I have pasted the above code in a vb file and then im calling this function in another file

newFile.vb contents....
Imports Project.JoinIntoPolyline.MyEditorExtensions
....
...
...
and calling the function
Call JoinIntoPolyline.Commands.MultipleJoin()

but it is throwing exception here

If Application.DocumentManager.IsApplicationContext Then
Throw New InvalidOperationException("Invalid execution context for Command()")

I have commented this line in code and was using

'<CommandMethod("MJOIN")> _.
Is this the issue.

Thanks
Gulzar
Message 10 of 11
_gile
in reply to: gulzar25

You cannot call the Editor.Command() extension method in an application context, it have to be called from a command without the CommandFlags.Session flag in the active Document.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 11 of 11
gulzar25
in reply to: _gile

Thanks a lot for the all the help.

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