.NET

Reply
Valued Contributor
Posts: 72
Registered: ‎10-17-2003
Message 11 of 16 (252 Views)

Re: Erasing Layouts

08-31-2012 05:53 AM in reply to: Artvegas

Hi ArtVegas,

 

I will try to better explain my question. I have a drawing with layout named SEQ001, SEQ002, SEQ003, SEQ004, ... I develop a palette with some buttons. On that palette I have a button to add a new layout. If the user add a layout number 3, I actually rename layout SEQ004 to SEQ005, SEQ003 to SEQ004 and insert a new layout named SEQ003. On the same palette I have another button to delete a layout. The current layout is delete and the subsequent layout are rename. Ex. suppressing SEQ003, SEQ004 is rename to SEQ003 and SEQ005 rename to SEQ004. Hera you will find the sub to rename sequences:

 

    Private Sub RenameSequence(ByVal SequenceNo As String, Optional ByVal Decrement As Boolean = False)
        Dim n As Integer = 0
        Dim i As Integer = 0
        Dim objSequence As PlanGeneration.RinsePath.Sequence = Nothing
        Dim strNewLayoutName As String = ""

        n = m_Sequences.SequenceList.Count ' m_sequences is a member variable containing information about layout
        If Decrement = False Then
            For i = n - 1 To 0 Step -1
                objSequence = m_Sequences.SequenceList(i)
                strNewLayoutName = "SEQ" & String.Format("{0:d3}", (CInt(objSequence.SeqNo.Substring(3)) + 1))
                RenameLayout(objSequence.SeqNo, strNewLayoutName)

               'I also rename some layers
                RenameLayer(objSequence.SeqNo & "-BORNE", strNewLayoutName & "-BORNE")
                RenameLayer(objSequence.SeqNo & "-ROUTE", strNewLayoutName & "-ROUTE")
                RenameLayer(objSequence.SeqNo & "-VANNE_OUVERTE", strNewLayoutName & "-VANNE_OUVERTE")
                RenameLayer(objSequence.SeqNo & "-VANNE_FERMEE", strNewLayoutName & "-VANNE_FERMEE")
                If objSequence.SeqNo.ToUpper = SequenceNo.ToUpper Then
                    Exit For
                End If
            Next i
        Else
            Dim j As Integer = GetSequenceOrder(SequenceNo)
            For i = j To n - 1
                objSequence = m_Sequences.SequenceList(i)
                strNewLayoutName = "SEQ" & String.Format("{0:d3}", (CInt(objSequence.SeqNo.Substring(3)) - 1))
                RenameLayout(objSequence.SeqNo, strNewLayoutName)

'I also rename some layers
                RenameLayer(objSequence.SeqNo & "-BORNE", strNewLayoutName & "-BORNE")
                RenameLayer(objSequence.SeqNo & "-ROUTE", strNewLayoutName & "-ROUTE")
                RenameLayer(objSequence.SeqNo & "-VANNE_OUVERTE", strNewLayoutName & "-VANNE_OUVERTE")
                RenameLayer(objSequence.SeqNo & "-VANNE_FERMEE", strNewLayoutName & "-VANNE_FERMEE")
                objSequence.SeqNo = strNewLayoutName

            Next
        End If

    End Sub

'Now the function to rename layout
    Private Function RenameLayout(ByVal oldLayoutName As String, ByVal NewLayoutName As String) As Boolean
        Dim bReturnValue As Boolean = True
        Dim oLayoutIDToRename As ObjectId = Nothing
        Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor()
        Dim Db As Database = ed.Document.Database()
        Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

        oLayoutIDToRename = GetLayoutByName(oldLayoutName)
        Try
            Using doclock As DocumentLock = acDoc.LockDocument
                Using tm1 As Transaction = Db.TransactionManager.StartTransaction()
                    Dim oLayoutToRename As Layout = tm1.GetObject(oLayoutIDToRename, OpenMode.ForWrite)
                    oLayoutToRename.LayoutName = NewLayoutName
                    tm1.Commit()
                    oLayoutToRename = Nothing
                End Using
            End Using

        Catch ex As Exception
            ed.WriteMessage("\nImpossible de renommer la présentation " & oldLayoutName & " à " & NewLayoutName & ": " & ex.Message)
            bReturnValue = False
        End Try

        Return bReturnValue

    End Function

 

When I use the button all is working well. But if the user delete manually the layout I want to use an event and rename the sequence too.

 

First I used this event:

<CommandMethod("AddLayoutToBeRemoved")> _
    Public Sub AddLayoutToBeRemoved()
        AddHandler LayoutManager.Current.LayoutToBeRemoved, _
            AddressOf onLayoutToBeRemoved
    End Sub

    Private Sub onLayoutToBeRemoved(ByVal senderObj As Object, _
                    ByVal docLayoutEvtArgs As Autodesk.AutoCAD.DatabaseServices.LayoutEventArgs)

        Dim strNextLayoutName As String = ""
        Dim bRenameLayout As Boolean = False

        If docLayoutEvtArgs.Name.ToUpper.StartsWith("SEQ") Then
            If GetLayoutList.Count > 1 Then
                'If m_CurrentSequencePosition < m_Sequences.SequenceList.Count - 1 Then
                '    strNextLayoutName = m_Sequences.SequenceList(m_CurrentSequencePosition + 1).SeqNo
                '    bRenameLayout = True
                'End If
                'Delete layers associated
                If DeleteLayer(m_Sequences.SequenceList(m_CurrentSequencePosition).SeqNo & "-BORNE", True) = False Then bRenameLayout = False
                If DeleteLayer(m_Sequences.SequenceList(m_CurrentSequencePosition).SeqNo & "-ROUTE", True) = False Then bRenameLayout = False
                If DeleteLayer(m_Sequences.SequenceList(m_CurrentSequencePosition).SeqNo & "-VANNE_OUVERTE", True) = False Then bRenameLayout = False
                If DeleteLayer(m_Sequences.SequenceList(m_CurrentSequencePosition).SeqNo & "-VANNE_FERMEE", True) = False Then bRenameLayout = False

                m_Sequences.SequenceList.RemoveAt(m_CurrentSequencePosition)
                If m_CurrentSequencePosition > m_Sequences.SequenceList.Count - 1 Then
                    m_CurrentSequencePosition = m_Sequences.SequenceList.Count - 1
                End If
                If m_CurrentSequencePosition < 0 Then
                    SetCurrentLayoutTab("Model")
                Else
                    SetCurrentLayoutTab(m_Sequences.SequenceList(m_CurrentSequencePosition).SeqNo)
                End If
                'If bRenameLayout = True Then
                '    RenameSequence(strNextLayoutName, True)
                '    SetCurrentLayoutTab(m_Sequences.SequenceList(m_CurrentSequencePosition).SeqNo)
                'End If

            End If
        End If

    End Sub

I realized this event occur before the layout is really deleted, so I understand why renaming my sequence doesn't work. I put some code in comments because it doesn't work. So I found this other event:

 

<CommandMethod("AddLayoutRemoved")> _
    Public Sub AddLayoutRemoved()
        AddHandler LayoutManager.Current.LayoutRemoved, _
            AddressOf onLayoutRemoved
    End Sub

 

    Private Sub onLayoutRemoved(ByVal senderObj As Object, _
                ByVal docLayoutEvtArgs As Autodesk.AutoCAD.DatabaseServices.LayoutEventArgs)

        Dim strNextLayoutName As String = ""
 
        If docLayoutEvtArgs.Name.ToUpper.StartsWith("SEQ") Then
            If GetLayoutList.Count > 1 Then
                If m_CurrentSequencePosition < m_Sequences.SequenceList.Count - 1 Then
                 Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.Regen()
                    strNextLayoutName = m_Sequences.SequenceList(m_CurrentSequencePosition).SeqNo
                    RenameSequence(strNextLayoutName, True)
                    SetCurrentLayoutTab(m_Sequences.SequenceList(m_CurrentSequencePosition).SeqNo)
                 End If
            End If
        End If

    End Sub

 

But I have the same error. When this event occurs the layout does't seem to be deleted too. So I don't know which event to use.

 

So actually, my user must absolutely use the delete layout button. If not, the sequence will be interrupt. If I don't find a solution, I will add another button to correct the sequence but it is not the way I preferred.

 

Thank you very much for your help.

 

Regards,

 

André

 

Distinguished Contributor
Artvegas
Posts: 104
Registered: ‎04-21-2011
Message 12 of 16 (242 Views)

Re: Erasing Layouts

08-31-2012 10:12 AM in reply to: andred

Hi Andre,

 

I noticed that your RenameSequence() function's "for-loop" seems to rename layouts from the largest number to the smallest (i.e. SEQ005 first, then SEQ004, then SEQ003, etc).

 

Lets say your user deletes SEQ003. Then the first thing your code appears to do is try to rename SEQ005 to SEQ004, but you can't because SEQ004 already exists. Perhaps this is the problem?

 

What you should do is get the number of the layout that is removed (say SEQ003), next rename SEQ004 to SEQ003, then rename SEQ005 to SEQ004, then SEQ006 to SEQ005, etc.

 

It's different when you add a layout. Say you're going to add a layout which will become SEQ003. In that case you should start at the biggest number (as per your for-loop). Here you would rename SEQ005 to SEQ006, then SEQ004 to SEQ005, then SEQ003 to SEQ004 and then add the new SEQ003.

 

You get what I mean?

 

Art

Valued Contributor
Posts: 72
Registered: ‎10-17-2003
Message 13 of 16 (238 Views)

Re: Erasing Layouts

08-31-2012 10:29 AM in reply to: Artvegas

Hi Art,

 

In my RenameSequence I have an argument Decrement and I have two loops depending of the state of this argument. When I add a layout, Decrement is false, so I begin with the higher sequence number and add 1 to the name. If I delete a layout, Decrement is set to true. I delete the layout and the next sequence is lower by 1 and so on. This routine is working well when I used your function to explicitly delete the layout. But when I start this function from the LayoutRemoved or LayoutToBeRemoved event, it doesn'work because, I don't know why, the layout is not already deleted so when I try to rename SEQ004 to SEQ003 (the one I deleted manually using AutoCAD command) SEQ003 already exist.

 

The problem is with the event. Maybe it is a bug in AutoCAD API.

 

Thank you,

 

André

 

Distinguished Contributor
Artvegas
Posts: 104
Registered: ‎04-21-2011
Message 14 of 16 (237 Views)

Re: Erasing Layouts

08-31-2012 10:42 AM in reply to: Artvegas

Sorry I didn't notice the decrement argument.

 

I created a simple test in C# for the LayoutRemoved event below. For simplicity this is based on layout names being numbers only, i.e. 001, 002, 003, 004, 005, etc.

 

This is not "well-coded" as it assumes that the order of the layouts in the layout-dictionary is sequential which may not always be the case, but this could easily be accounted for with a bit more work.

 

This worked for me when I tested it. So I don't think it's an AutoCAD bug. Could it be something else in your program?

 

 

 

// Command.
[CommandMethod("RegisterLayoutRemoved")]
static public void RegisterLayoutRemoved()
{
	// Register layout removed event.
	LayoutManager.Current.LayoutRemoved
		+= new LayoutEventHandler(Current_LayoutRemoved);
}

// Layout removed event handler.
static void Current_LayoutRemoved(object sender, LayoutEventArgs e)
{
	Document doc = Application.DocumentManager.MdiActiveDocument;
	Database db = doc.Database;
	Editor ed = doc.Editor;

	// Get removed layout number from layout name.
	int removedNum;
	if (!Int32.TryParse(e.Name, out removedNum))
	{
		throw new System.Exception("Could not obtain layout number...");
	}

	using (Transaction tr = db.TransactionManager.StartTransaction())
	{
		// Iterate layout dictionary.
		DBDictionary layoutDict = (DBDictionary)tr.GetObject(
			db.LayoutDictionaryId, OpenMode.ForRead, true);
		foreach (DBDictionaryEntry item in layoutDict)
		{
			// Layout (for read).
			Layout layout
				= (Layout)tr.GetObject(item.Value, OpenMode.ForRead);

			// Try get layout number.
			int i;
			if (Int32.TryParse(layout.LayoutName, out i))
			{
				// If layout number is greater than for layout removed.
				if (i > removedNum)
				{
					// Then decrement layout number.
					layout.UpgradeOpen();
					layout.LayoutName
						= String.Format("{0:D3}", (i - 1));
				}
			}
		}

		tr.Commit();
	}
}

 

 

Valued Contributor
Posts: 72
Registered: ‎10-17-2003
Message 15 of 16 (232 Views)

Re: Erasing Layouts

08-31-2012 10:48 AM in reply to: Artvegas

Thank you very much Art,

 

I will try that and I will give you some feedback.

 

Have a good day,

 

André

 

Distinguished Contributor
Posts: 404
Registered: ‎11-11-2002
Message 16 of 16 (228 Views)

Re: Erasing Layouts

08-31-2012 01:39 PM in reply to: s.jabs

Below is a partial of a routine that I use in VBA to remane layouts.

 

You should first remane the layouts, so you don't have have a conflict about remaning layout to existing layouts.

I first added temp to the layout name.

 

Then I use the TabOrder property of the layout to get the order of the layouts.

 

Now you can reanme the layout to a sequence of names based on the TabOrder of the layouts.

 

http://www.theswamp.org/index.php?action=search2

 

 

 

**************** vba routine not vb.net *****************************

 

 

For Each objLayout In objLayouts
    
     lngCnt = lngCnt + 1
     strLayoutName = objLayout.Name
         If strLayoutName <> "Model" Then
             lngTabOrder = objLayouts(objLayout.Name).TabOrder
             lngTabOrder = lngTabOrder + (formRenumber.TxtBoxStartNumber.Value - 1)
             objLayout.Name = "tempLayout" & lngTabOrder
             DoEvents
         End If

    Call ProgUpdate(lngCnt)
    DoEvents

Next

Call ProgSetup(objLayouts.Count)

formProgRenumLay.Caption = " Re-Naming objLayouts "

lngCnt = 0
For Each objLayout In objLayouts
     lngCnt = lngCnt + 1
     strLayoutName = objLayout.Name
         If strLayoutName <> "Model" Then
            objLayout.Name = formRenumber.txtBoxLayName & _
            CStr(Mid$(strLayoutName, 11, (Len(strLayoutName) - 10))) ' Rename and renumber objLayout
            DoEvents
         End If

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community