Author Topic: How to insert a drawing via Ribbon Bar Button?  (Read 5752 times)

0 Members and 1 Guest are viewing this topic.

deathman20

  • Guest
How to insert a drawing via Ribbon Bar Button?
« on: January 21, 2011, 02:53:38 PM »
Hello all,

Im new to coding and have created a ribbon bar now in .net though, been having a lovely time trying to work this one out but hitting road blocks.  Below is my original thread I did on the Autodesk forums.

http://forums.autodesk.com/t5/NET/How-to-insert-a-drawing-via-Ribbon-Bar-Button/m-p/2887928

Code: [Select]
   Private Sub addBCM1Chain(ByVal ribTab As RibbonTab)
        'Create the panel source
        Dim ribSourcePanel As RibbonPanelSource = New RibbonPanelSource()
        ribSourcePanel.Title = "1 Chain"
        'Create the panel
        Dim ribPanel As New RibbonPanel()
        ribPanel.Source = ribSourcePanel
        ribTab.Panels.Add(ribPanel)
        'Create a button
        Dim ribBtn As Autodesk.Windows.RibbonButton = New RibbonButton()
        ribBtn.Text = "Net load"
        ribBtn.CommandParameter = ""
        ribBtn.Orientation = Windows.Controls.Orientation.Vertical
        ribBtn.Size = RibbonItemSize.Large
        ribBtn.LargeImage = LoadImage("Block.png")
        ribBtn.ShowImage = True
        ribBtn.ShowText = True
        ribBtn.CommandHandler = New AdskCommandHandler()
        'Add the button
        ribSourcePanel.Items.Add(ribBtn)
    End Sub

The "ribBtn.CommandParameter = " is where im having issues with.

If I try this command after the = "-Insert Block"
It gets it going in the right direction.  I can select a spot to drop the block on the drawing but then I have all the scale stuff to pass through.

If I try this command after the =  "-Insert Block" & vbCrLf & "1" & vbCrLf & "1" & vbCrLf
Now it posts it but I don't get to select an insertion point.  It picks some space where the last time your cursor clicked the model space.

Now if i try this command after the = "Insert " & "Block"
It brings up the block inserting window.  Though not sure if this is how exactly the macro works or not that I have currently used inside autocad (which you can easily insert more blocks of the same type by right clicking).  Though when the command is done it brings up the window, sits there at it (no text being put into the block line) and takes a few escapes and such to back out then runs the "Block" part of the command.

Trying to find a way to insert my macro code or something similar where I can click a button it gets the block, I can select where to put it in the drawing & have the option to rotate or select a new base point before it gets inserted into the drawing.  Something similar if there you where using a tool pallet.

Thanks in advance.
« Last Edit: January 27, 2011, 01:48:38 PM by deathman20 »

deathman20

  • Guest
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #1 on: January 26, 2011, 03:35:56 PM »
Maybe found a way to do it properly using Tony's Commandline.

http://www.caddzone.com/CommandLine.vb

Though now I have an issue and don't know where to go from here.

'Commandline' is not a member of 'Autodesk.Windows.RibbonButton'

So how would I add the Commandline to function in part with the Autodesk.Windows.RibbonButton (etc since there are more commands out there like RibbonSplitButton)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #2 on: January 26, 2011, 07:58:17 PM »

deathman20

  • Guest
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #3 on: January 27, 2011, 10:16:06 AM »
Hi Jeff,

Yes the class is how I got this far already but im stuck with the ribBtn.CommandParameter right now.

The CommandParameter doesn't allow for a very nice way of trying to insert a block that I want to manipulate still, like selecting a point to drop it in the drawing and possible rotation.  Scale I don't want to touch.  Though using Tony's Commandline class that I've added to the project it works perfectly (somethings I still have to figure out) but it inserts it as I want it to.  Now I just need to tie that to the ribbon button.

Though the Commandline class isn't a member of the Autodesk.Windows.RibbonButton functions.  How would I go about adding the Class into that member group so I can use it as im intending.

Basically replacing this "ribBtn.CommandParameter" with "ribBtn.CommandLine"

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #4 on: January 27, 2011, 10:40:46 PM »
You do have this class implemented?



Code: [Select]

Public Class AdskCommandHandler
  Implements System.Windows.Input.ICommand

  Public Function CanExecute(ByVal parameter As Object) _
  As Boolean Implements System.Windows.Input.ICommand.CanExecute
    Return True
  End Function

  Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
  Implements System.Windows.Input.ICommand.CanExecuteChanged

  Public Sub Execute(ByVal parameter As Object) _
  Implements System.Windows.Input.ICommand.Execute
    Dim ribBtn As RibbonButton = TryCast(parameter, RibbonButton)
    If ribBtn IsNot Nothing Then
      acApp.DocumentManager.MdiActiveDocument.SendStringToExecute( _
          ribBtn.CommandParameter, True, False, True)
    End If
  End Sub
End Class

This will send the string value stored in CommandParameter to the commandline




deathman20

  • Guest
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #5 on: January 28, 2011, 09:37:14 AM »
Here managed to clean up the code to show you.

Its just the inserting function that is bugging me.  I can't get it to work as desired.

Jeff,

I'll check that out some more, I know mine is a little different from pulling it from a ribbon sample that was put out at AU and haven't totally cleaned it all up from it.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #6 on: January 28, 2011, 10:40:34 AM »
I would forget about the ribbon part and focus on creating a method with a CommandMethod attribute for inserting the block(for example named "insertconveyer")

then all you have to do set "insertconveyer" as the CommandParameter.



Start hitting buttons on the ribbon and watch the commandline it just sending commands to it

deathman20

  • Guest
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #7 on: January 28, 2011, 10:45:21 AM »
Yeah I did notice that the other day, though... well im new to this.

Im figuring minor things out as I go with trial and error so far but I haven't made anything from scratch.

I know I would need to make a new class is my guess to help with the command but I have no clue where to start and how to pull information from the button being pressed (to get the block name) and to run with it.

Any hints or suggestions?

I know im so close to getting this workable.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #8 on: January 28, 2011, 11:00:27 AM »
You could set the command parameter to  "insertconveyer" to try to insert a block named 01M_Antriebsst_1000 is what I think you are trying to do


does this code make sense?


Code: [Select]
  <CommandMethod("insertconveyer")> _
        Public Sub insertconveyer()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor

            Using trx As Transaction = db.TransactionManager.StartTransaction()

                Dim bt As BlockTable = db.BlockTableId.GetObject(OpenMode.ForRead)
                Dim btrMs As BlockTableRecord = bt(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForRead)
             
                Dim btrObjId As ObjectId = bt("01M_Antriebsst_1000").GetObject(OpenMode.ForRead).ObjectId


                Dim bref As New BlockReference(ed.GetPoint(vbLf & "Select Point").Value, btrObjId)
               
                btrMs.UpgradeOpen()
                btrMs.AppendEntity(bref)
                trx.AddNewlyCreatedDBObject(bref, True)
                trx.Commit()
                ed.WriteMessage(vbLf & "Block Reference Inserted")
            End Using

        End Sub


deathman20

  • Guest
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #9 on: January 28, 2011, 11:46:22 AM »
Correct.  Trying to get it to insert what I want to tell it.  Could be 1 of over 2000+ names that is defined in the button, really the button text (ribBtn.Text = "01M_Antriebsst_0500")

Trying it right now but all im coming back with is Unknown Command "INSERTCONVEYOR"

Trying to play with it some more.  Maybe I have it in the wrong location.  Im putting it under the Public Class ConveyorRibbon.
« Last Edit: January 28, 2011, 12:22:29 PM by deathman20 »

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #10 on: January 28, 2011, 07:46:15 PM »
This is quick way to go at it but maybe will give you a idea


commented out line for images because was causing it to fail

have a drawing with the 01M_Antriebsst_1000 & 01M_Antriebsst_500 block
netload then click buttons and pick insertion point


no jigging or error handling again just to give you a idea

deathman20

  • Guest
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #11 on: January 30, 2011, 07:05:33 PM »
Thanks Jeff,

Yes managed to get it to run just a few min ago with the previous code you posted.  Must of missed something the first time I tried it.

Though im trying to figure out how to get it to not look in the block database.  These are separate drawings located in a referenced directory on the harddrive.  They will not be in the drawing unless its put in there.  Otherwise drawings would be in the 300-350 meg range to start off, which wouldn't be cool.

Really do appreciate this greatly!

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #12 on: January 30, 2011, 07:11:09 PM »
Do a search for WblockCloneObjects there are plenty of examples of grabbing blocks from external databases

Jeff H

  • Needs a day job
  • Posts: 6150
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #13 on: January 30, 2011, 07:15:46 PM »

deathman20

  • Guest
Re: How to insert a drawing via Ribbon Bar Button?
« Reply #14 on: January 31, 2011, 12:39:20 PM »
Thanks Jeff

Had a nice set back for my work laptop.  Decided to do the click of death this morning on boot up.  So now Im waiting to get hopefully my projects of the drive.