.NET

.NET

Reply
Active Contributor
gulzar25
Posts: 42
Registered: ‎07-02-2013
Message 1 of 11 (317 Views)
Accepted Solution

Filtered objects into a single polyline

317 Views, 10 Replies
07-11-2013 08:08 PM

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

 

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.

*Expert Elite*
_gile
Posts: 2,127
Registered: ‎04-29-2006
Message 2 of 11 (298 Views)

Re : Filtered objects into a single polyline

07-11-2013 11:38 PM 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
Active Contributor
gulzar25
Posts: 42
Registered: ‎07-02-2013
Message 3 of 11 (291 Views)

Re : Filtered objects into a single polyline

07-12-2013 12:03 AM 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
*Expert Elite*
_gile
Posts: 2,127
Registered: ‎04-29-2006
Message 4 of 11 (281 Views)

Re : Filtered objects into a single polyline

07-12-2013 12:27 AM 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
Active Contributor
gulzar25
Posts: 42
Registered: ‎07-02-2013
Message 5 of 11 (274 Views)

Re : Filtered objects into a single polyline

07-12-2013 12:45 AM 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
*Expert Elite*
_gile
Posts: 2,127
Registered: ‎04-29-2006
Message 6 of 11 (266 Views)

Re : Filtered objects into a single polyline

07-12-2013 01:16 AM 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
Active Contributor
gulzar25
Posts: 42
Registered: ‎07-02-2013
Message 7 of 11 (259 Views)

Re : Filtered objects into a single polyline

07-12-2013 01:43 AM 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
*Expert Elite*
_gile
Posts: 2,127
Registered: ‎04-29-2006
Message 8 of 11 (257 Views)

Re : Filtered objects into a single polyline

07-12-2013 02:14 AM 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
Active Contributor
gulzar25
Posts: 42
Registered: ‎07-02-2013
Message 9 of 11 (249 Views)

Re : Filtered objects into a single polyline

07-12-2013 03:50 AM 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
*Expert Elite*
_gile
Posts: 2,127
Registered: ‎04-29-2006
Message 10 of 11 (242 Views)

Re : Filtered objects into a single polyline

07-12-2013 05:00 AM 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
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.