{"id":481,"date":"2019-12-13T13:13:02","date_gmt":"2019-12-13T13:13:02","guid":{"rendered":"https:\/\/oursoide.wordpress.com\/?p=481"},"modified":"2019-12-13T13:13:02","modified_gmt":"2019-12-13T13:13:02","slug":"un-programme-vba-pour-editer-les-proprietes-de-plusieurs-plans-inventor-a-partir-dexcel","status":"publish","type":"post","link":"https:\/\/blog.nanuq.me\/?p=481","title":{"rendered":"[ARCHIVE] Un programme VBA pour \u00e9diter les propri\u00e9t\u00e9s de plusieurs plans Inventor \u00e0 partir d&rsquo;Excel"},"content":{"rendered":"<p><em>\ud83d\uddd3\ufe0f Note : Cet article est une archive import\u00e9e de mon ancien blog. Certaines informations peuvent ne plus \u00eatre \u00e0 jour.<\/em><\/p>\n<p>Voici un petit programme en VBA pour importer \/ \u00e9diter \/ exporter les propri\u00e9t\u00e9s d&rsquo;un plan entre Inventor et Excel<\/p>\n<p><!--more--><\/p>\n<p><a href=\"https:\/\/blog.nanuq.me\/wp-content\/uploads\/2019\/12\/sergey-zolkin-m9qmoh-scfe-unsplash.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-486 size-large\" title=\"\" src=\"https:\/\/blog.nanuq.me\/wp-content\/uploads\/2019\/12\/sergey-zolkin-m9qmoh-scfe-unsplash.jpg?w=670\" alt=\"\" width=\"670\" height=\"447\"><\/a><\/p>\n<p>Bonjour.<\/p>\n<p>Dans la vraie vie de la r\u00e9alit\u00e9 vraie, je suis dessinateur industriel et j&rsquo;utilise couramment Autodesk Inventor. C&rsquo;est pourquoi j&rsquo;ai cr\u00e9\u00e9 ce petit programme&#8230;<\/p>\n<p>Or, il m&rsquo;arrive souvent d&rsquo;avoir \u00e0 \u00e9diter le cartouche d&rsquo;une dizaine de plans simultan\u00e9ment (par exemple, renvoyer tout un jeu de plan avec la date du jour). Et il se trouve que dans ma soci\u00e9t\u00e9, il a \u00e9t\u00e9 d\u00e9cid\u00e9 qu&rsquo;une page = un document ; c\u00e0d 1 sheet = 1 dwg . (Bien qu&rsquo;Inventor peut g\u00e9rer plusieurs sheet dans le m\u00eame fichier mais passons&#8230;)<\/p>\n<p>Aussi, les informations du cartouche sont d\u00e9finis dans les propri\u00e9t\u00e9s du plan. C&rsquo;est donc barbant de modifier chaque champs \u00e0 la main et de faire \u00e7a pour chaque sheet.<\/p>\n<p>J&rsquo;ai donc cr\u00e9\u00e9 ce petit programme qui permet d&rsquo;extraire pour chaque document ouvert sur Inventor, toutes les propri\u00e9t\u00e9s et d&rsquo;afficher \u00e7a sous forme de tableau sur Excel. On peut ainsi tout modifier et tout r\u00e9injecter dans Inventor.<\/p>\n<p>Bon, c&rsquo;est cod\u00e9 avec les pieds, y&rsquo;a certainement pas mal d&rsquo;erreurs &#8230; N\u00e9anmoins \u00e7a marche. Et si c&rsquo;est pas cass\u00e9 pourquoi le r\u00e9parer ?<\/p>\n<p>N&rsquo;h\u00e9sitez pas \u00e0 me faire vos retours !<\/p>\n<p>Le code est l\u00e0 en dessous (pour vous \u00e9viter de t\u00e9l\u00e9charger un vilain .xlsm)<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-489\" title=\"\" src=\"https:\/\/blog.nanuq.me\/wp-content\/uploads\/2019\/12\/screen.png\" alt=\"SCREEN\" width=\"1127\" height=\"631\"><\/p>\n<pre>Option Explicit\n'DECLARATION DES VARIABLES \"PUBLIC\"\n\nPublic inventorApp As Inventor.Application\nPublic iDoc As Inventor.Document\n\nPublic CustomPropertySet As PropertySet\n\nPublic str_propname As String\n\nPublic j As Double\nPublic k As Double\n\nPublic lRow As Integer\nPublic lCol As Integer\n\nSub ExtractPropInventor()\n\n'========== CALCUL DU TEMPS D'EXECUTION ==========\n' D\u00e9clarations\nDim temps_debut As Single\nDim duree As Single\nDim doc As Document\n\n' R\u00e9cup\u00e9rer le temps initial\ntemps_debut = Timer\n'=========================================================\n\n'Stop refresh\nApplication.ScreenUpdating = False\n\n'Variables -&gt; voir \"module_VARIABLES\"\n\n'Passage de la table en type de donn\u00e9es TEXT\nCells.Select\nSelection.NumberFormat = \"@\"\n\n'Connection \u00e0 une instance inventor existante\nOn Error Resume Next\nSet inventorApp = GetObject(, \"Inventor.Application\")\n'Si inventor n'est pas ouvert, message d'erreur\nIf Err Then\nMsgBox \"Veuillez ouvrir une instance Inventor.\"\nExit Sub\nEnd If\n\n' Inventor Visible :\ninventorApp.Visible = True\n\n'RECUPERATION DES PROPRIETES\n' Balayage de tous les documents ouvert (ipt,iam,dwg...)\n' Reset de la variable colonne\nk = 3\nFor Each iDoc In inventorApp.Documents\n'Si le document est un dessin alors\nIf iDoc.DocumentType = kDrawingDocumentObject Then\n' R\u00e9cup\u00e9ration du titre du dessin pour titre de la colonne\nCells(1, k).value = iDoc.DisplayName\n' R\u00e9cup\u00e9ration des propri\u00e9t\u00e9s utilisateurs\nSet CustomPropertySet = iDoc.PropertySets.item(\"Inventor User Defined Properties\")\n' Reset de la variable ligne\nj = 2\n' Pour chaque champs dans la liste des propri\u00e9t\u00e9s utilisateurs\nFor Each item In CustomPropertySet\n'on cherche si la propri\u00e9t\u00e9 existe d\u00e9ja , si elle existe, on \u00e9crit la valeur\nOn Error Resume Next\nfindedRow = WorksheetFunction.Match(item.Name, Range(\"B:B\"), 0)\nIf findedRow &lt;&gt; 0 Then\nCells(findedRow, k).value = item.value\n'reset de findedRow\nfindedRow = 0\nElse\n'sinon on cr\u00e9e la propri\u00e9t\u00e9 tout en bas du tableau\n'on cherche la derni\u00e9re ligne\nlRow = Cells(Rows.Count, 2).End(xlUp).Row\n'que l'on incr\u00e9mente de 1 pour avoir une ligne vierge\nlRow = lRow + 1\nCells(lRow, 2).value = item.Name\nCells(lRow, k).value = item.value\n'reset\nfindedRow = 0\nEnd If\n\nNext item\n'colonne suivante\nk = k + 1\nEnd If\nNext iDoc\n\n'Ecriture de la cellule A1\nRange(\"B1\").value = \"PROPRIETES\"\n\n'Ajustement automatique de la largeur des colonnes\n'Passage de la table en type de donn\u00e9es TEXT\nCells.Select\nCells.EntireColumn.AutoFit\nSelection.NumberFormat = \"@\"\n\n'Suppression du jaune\nCells.Select\nWith Selection.Interior\n.Pattern = xlNone\n.TintAndShade = 0\n.PatternTintAndShade = 0\nEnd With\nRange(\"A1\").Select\n\n'Reactive refresh\nApplication.ScreenUpdating = True\n\n'========== CALCUL DU TEMPS D'EXECUTION ==========\n' Calculer la dur\u00e9e d'ex\u00e9cution\nduree = Timer - temps_debut\n\n' Afficher la dur\u00e9e d'ex\u00e9cution\n\nMsgBox (\"EXTRACT REUSSI ! Dur\u00e9e d'\u00e9x\u00e9cution : \" &amp; duree)\n'=========================================================\n\nEnd Sub\n\nSub InsertPropInventor()\n\n'========== CALCUL DU TEMPS D'EXECUTION ==========\n' D\u00e9clarations\nDim temps_debut As Single\nDim duree As Single\nDim doc As Document\n\n' R\u00e9cup\u00e9rer le temps initial\ntemps_debut = Timer\n'=========================================================\n\n'Stop refresh\nApplication.ScreenUpdating = False\n\n'Variables -&gt; voir \"module_VARIABLES\"\nDim customProp As Property\n\n'Connection \u00e0 une instance inventor existante\nOn Error Resume Next\nSet inventorApp = GetObject(, \"Inventor.Application\")\n'Si inventor n'est pas ouvert, message d'erreur\nIf Err Then\nMsgBox \"Veuillez ouvrir une instance Inventor.\"\nExit Sub\nEnd If\n\n' Inventor Visible :\ninventorApp.Visible = True\n\n'd\u00e9sactivation du screen update\ninventorApp.ScreenUpdating = False\n\n'DETERMINATION DES LIMITES DU TABLEAU\n'Recherche de la derni\u00e9re ligne sur la colonne 1\nlRow = Cells(Rows.Count, 2).End(xlUp).Row\n\n'Recherche de la derni\u00e9re colonne sur la ligne 1\nlCol = Cells(1, Columns.Count).End(xlToLeft).Column\n\n'INSERTION DES PROPRIETES\n'reset des variables\nk = 3\n\n'Balayage tous les documents ouverts sur inventor (ipt,iam,dwg)\nFor Each iDoc In inventorApp.Documents\n'Si le document est un dessin alors\nIf iDoc.DocumentType = kDrawingDocumentObject Then\n'Balayage du tableau excel\nFor k = 3 To lCol\n'Si le nom du document correspond au nom de la cellule\nIf iDoc.DisplayName = Cells(1, k).value Then\n'd\u00e9finition de la variable CustomPropertySet\nSet CustomPropertySet = iDoc.PropertySets.item(\"Inventor User Defined Properties\")\n'Sur chaque ligne du tableau\nFor i = 2 To lRow\n'on r\u00e9cup\u00e9re le nom de la propri\u00e9t\u00e9\nstr_propname = Cells(i, 2).value\n'On v\u00e9rifie si la cellule a \u00e9t\u00e9 modifi\u00e9 (via sa couleur)\nIf Cells(i, k).Interior.ColorIndex = 6 Then\n'si la cellule contenant la valeur est vide, alors on ne met rien dans la propri\u00e9t\u00e9\nIf IsEmpty(Cells(i, k)) Then\nCustomPropertySet.item(str_propname).value = vbNullString\n'sinon on met la valeur souhait\u00e9e\nElse\nCustomPropertySet.item(str_propname).value = Cells(i, k).value\nEnd If\n'Si la cellule n'a pas \u00e9t\u00e9 modifi\u00e9, on passe \u00e0 la ligne suivante\nElse\nEnd If\nNext i\nEnd If\nNext k\nEnd If\nNext iDoc\n\n'r\u00e9activation du screen update\ninventorApp.ScreenUpdating = True\n\n'Reactive refresh\nApplication.ScreenUpdating = True\n\n'========== CALCUL DU TEMPS D'EXECUTION ==========\n' Calculer la dur\u00e9e d'ex\u00e9cution\nduree = Timer - temps_debut\n\n' Afficher la dur\u00e9e d'ex\u00e9cution\nMsgBox (\"INSERT REUSSI ! Dur\u00e9e d'\u00e9x\u00e9cution : \" &amp; duree)\n'=========================================================\n\nEnd Sub<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\uddd3\ufe0f Note : Cet article est une archive import\u00e9e de mon ancien blog. Certaines informations peuvent ne plus \u00eatre \u00e0 jour. Voici un petit programme&#8230;<\/p>\n","protected":false},"author":1,"featured_media":486,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,4],"tags":[19,31,51,66,95,122],"_links":{"self":[{"href":"https:\/\/blog.nanuq.me\/index.php?rest_route=\/wp\/v2\/posts\/481"}],"collection":[{"href":"https:\/\/blog.nanuq.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nanuq.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nanuq.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nanuq.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=481"}],"version-history":[{"count":0,"href":"https:\/\/blog.nanuq.me\/index.php?rest_route=\/wp\/v2\/posts\/481\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.nanuq.me\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/blog.nanuq.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nanuq.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nanuq.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}