Here the full prog.
just copy and past in vba
run the macro : Procgen
Sorry it is in french. But if you need explain i can for you 🙂
you just need to replace "c:\" with the path of your bloc XREF.
etap :
1 - find all the bloc INSERT
2 - test if the bloc is a Xref. (isXref)
3 -if it is so check the bloc def in database (.block())
4 - rename the path with : "c:\" + Name of block + ".dwg"
5 - update the bloc.
6 - end
you just need to save the file , close and reopen --> xref updated !!
i am tested this and it works. (iam under acad 2009)
(it work if you name of bloc = name of file.)
bye
'**********************************************************************
'*** function de sélection selon des critères**************************
'**********************************************************************
' ENTREE :
' Nom de la sélection
' Liste de format :
' ex : (0 , "INSERT", 2, "POSTE") Toujours par 2 : CODE DXF, TYPE RECHERCHE
';**********************************************************************
Public Function SelectAll(NomSelection As String, ParamArray Element()) As Variant
ReDim FiltreT(0 To ((UBound(Element) - 1) / 2)) As Integer '--> conteneur Type de filtre
Dim FilterType As Variant '--> Type de filtre ex 0 : filtre sur le type d'objet
ReDim FiltreD(0 To ((UBound(Element) - 1) / 2)) As Variant '--> conteneur Nom de l'objet
Dim FilterData As Variant '--> nom de l'objet pour le filtre
Dim index As Integer
Dim Selection As AcadSelectionSet
Dim erreur As Long
index = 0
If NomSelection = "" Then NomSelection = "SelectionAuto"
On Error Resume Next
Set Selection = ThisDrawing.SelectionSets.Add(NomSelection)
If Err.Number <> 0 Then
Set Selection = ThisDrawing.SelectionSets(NomSelection)
End If
Selection.Delete
Set Selection = ThisDrawing.SelectionSets.Add(NomSelection)
On Error GoTo 0
index = 0
While index < UBound(Element)
FiltreT(index \ 2) = Element(index)
FiltreD(index \ 2) = Element(index + 1)
index = index + 2
Wend
FilterType = FiltreT
FilterData = FiltreD
Selection.Select acSelectionSetAll, , , FilterType, FilterData
Set SelectAll = Selection
End Function
Public Sub Procgen()
Dim OkErreur As Boolean '--> Retour de la fonction si erreur (=1)
OkErreur = False
Dim ListBloc As AcadSelectionSet '--> liste des éléments d'un sélection
Dim Bloc As Variant '--> boucle sur les sélections
Dim DefBloc As AcadBlock '--> Référence de l'insertion du bloc
'===========================================
'===== récuperation des blocs "INSERT" =====
'===========================================
Set ListBloc = SelectAll("CHANGEXREF", 0, "INSERT")
'===========================================
'======= Boucle sur chaque élément =========
'===========================================
For Each Bloc In ListBloc 'pour chaque bloc dans la sélection (dans le dessin)
Set DefBloc = ThisDrawing.Blocks(Bloc.Name)
If DefBloc.IsXRef Then '--> c'est un xref
OkErreur = BoucleRechargeXref(Bloc) '--> Récursivité <--
If OkErreur Then
Exit For
End If
End If 'si c'est un xref
Next
If OkErreur Then
MsgBox MessageErreurXref, vbCritical + vbOKOnly, "Erreur XRefChange"
End If
End Sub
Private Function BoucleRechargeXref(InsertBloc As Variant) As Boolean
Dim OkErreur As Boolean '--> Retour de la fonction si erreur (=1)
OkErreur = False
Dim Ok As Boolean
Ok = False
Dim RetourChariot As String
RetourChariot = Chr(13) + Chr(10)
Dim ListBloc As AcadSelectionSet
Dim Bloc As Variant '--> boucle sur les sélection
Dim DefBlocXRef As AcadExternalReference '--> définition du bloc ou du xref
Dim DefBloc As AcadBlock '--> Référence de l'insertion du bloc
Dim RepDessin As String '--> répertoire du dessin en cours
Dim RepXRefOld As String '--> répertoire du Xref trouvé (ancien)
Dim RepXRefTrouver As String '--> répertoire du XRef trouvé (nouveau)
Dim OkMaJ As Boolean '--> la mise à jour du Répertoire est OK
OkMaJ = False
'===========================================
'====== Traitement du Bloc Xref ============
'===========================================
Set DefBlocXRef = InsertBloc
RepXRefOld = DefBlocXRef.Path
Set DefBloc = ThisDrawing.Blocks(InsertBloc.Name)
RepDessin = ThisDrawing.GetVariable("DWGPREFIX")
ThisDrawing.Utility.Prompt RetourChariot + Separateur + "Xref trouvé : " + DefBlocXRef.Name
ThisDrawing.Utility.Prompt RetourChariot + Separateur + "-->Rep : " + RepertoireXRef
RepXRefTrouver = "c:\" & DefBloc.Name & ".dwg"
'===========================================
'===Mise à jour du répertoire ==============
'===========================================
If RepXRefTrouver <> "" Then '--- Ok trouver alors mise à jour -----
OkMaJ = True
DefBlocXRef.Path = RepXRefTrouver
DefBlocXRef.Update
Else
OkErreur = True
MessageErreurXref = "Répertoire introuvable : " + RetourChariot + _
RepXRefOld + RetourChariot + _
"pour l'xref : " + DefBlocXRef.Name + RetourChariot + _
String(Len(ThisDrawing.GetVariable("DWGPREFIX")) + 20, "=") + RetourChariot + _
"dans le fichier : " + ThisDrawing.GetVariable("DWGNAME") + RetourChariot + _
"Répertoire : " + ThisDrawing.GetVariable("DWGPREFIX")
End If
ThisDrawing.Utility.Prompt RetourChariot
BoucleRechargeXref = OkErreur
End Function