ilogic rule that creating a cope of a drawing and changing its iPart instance.

ilogic rule that creating a cope of a drawing and changing its iPart instance.

Anonymous
Not applicable
1,072 Views
13 Replies
Message 1 of 14

ilogic rule that creating a cope of a drawing and changing its iPart instance.

Anonymous
Not applicable

Hello, i have a model with a lot of iPart variants and i was wondering if it's possible to create a iLogic rule that copes a reference drawing, changes its iPart instance and then saves it.

 

 

 

  

  

0 Likes
Accepted solutions (1)
1,073 Views
13 Replies
Replies (13)
Message 2 of 14

FINET_Laurent
Advisor
Advisor

Work flow whould be (from what I understood) :

- Look through the iParts in the assembly and get each part file names.

- Open an existing drawing

- Replace the existing views with new views for each referenced iPart

- Save the drawing and close it

 

..is it correct ?

 

Regards,

FINET L.

 

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

Message 3 of 14

Anonymous
Not applicable
yes, that would be the idea
0 Likes
Message 4 of 14

WCrihfield
Mentor
Mentor

Perhaps this iLogic rule code will work for you.  I've included a lot of comment lines within it to help guide you through what's going on.

You WILL have to edit the middle portion of the code, where you need to define which other iPart you want to be represented within the drawing.  I simply gave a generic manual name here.  I assume this new iPart doesn't already have its own drawing file, or you wouldn't be creating one for it.  I also didn't bother building a list of all the iParts that were already within the current assembly, because I literally have no idea if the new iPart is even in the assembly yet, or how you want to specify it.

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Assembly Documents.",vbOKOnly, "WRONG DOCUMENT TYPE")
	Exit Sub 'or Return
End If

Dim oADoc As AssemblyDocument = ThisAssembly.Document
Dim oRefDoc As Document
'get an iPart within the assembly
For Each oRefDoc In oADoc.AllReferencedDocuments
	If oRefDoc.DocumentType = DocumentTypeEnum.kDesignElementDocumentObject Then Exit For
Next
'get the path where this iPart's file is found.
Dim oPath As String = IO.Path.GetDirectoryName(oRefDoc.FullFileName) & "\"

'define which other iPart you want to be represented within the new drawing.
Dim oNewModelName As String = "NewModel"
Dim oNewPart As String = oPath & oNewModelName & ".ipt"
Dim oNewDrawing As String = oPath & oNewModelName & ".idw"

If IO.File.Exists(oRefDoc.FullFileName.Replace(".ipt", ".idw")) Then
	'open the existing drawing for that iPart (assuming it has the same name and stored in the same directory)
	Dim oDDoc As DrawingDocument = ThisApplication.Documents.Open(oRefDoc.FullFileName.Replace(".ipt", ".idw"), False)
	'save a new copy of the file to disk, then close this old file
	oDDoc.SaveAs(oNewDrawing, True) 'saves a new copy of it to disk, while leaving the old file open
	oDDoc.Close(True) 'True  = skip save (this is the old document, so we don't want to save it.
	'now open the new copy of the drawing
	Dim oNewDDoc As DrawingDocument = ThisApplication.Documents.Open(oNewDrawing, False)
	'get the current file descriptor being referenced by this drawing
	Dim oFD As FileDescriptor = oNewDDoc.ReferencedDocumentDescriptors.Item(1).ReferencedFileDescriptor
	'replace it with the full file name of our new iPart's file.
	oFD.ReplaceReference(oNewPart)
	'update the drawing document
	oNewDDoc.Update()
	oNewDDoc.Save
	oNewDDoc.Close(True)
End If

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

If you have time, please... Vote For My IDEAS 💡and Explore My CONTRIBUTIONS

Inventor 2020 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 14

Anonymous
Not applicable

Sorry i am fairly new to iLogic and i didn't quite understand how to use it, is it correct how i filled it? (shown in the screenshot)

1.PNG

 

My idea was more of making a Ilogic/Macro/Script that makes a copy of oppend drawing, switch to the next iPart instance and saves it with the iPart instance name. thank you for your help and time.

0 Likes
Message 6 of 14

WCrihfield
Mentor
Mentor
Accepted solution

Sorry it took so long to get back to you, I've been really busy.

Within the image you posted:

- the iPart location you put in there should replace the "IO.Path.GetDirectoryName(oRefDoc.FullFileName)", instead of going behind it.

- your iPart name edit seems fine.

   - Just checking though...that iPart does have a model file of its own saved out to disk right?  (It's not just data on the iPart table, with no physical model that has been saved out to its own file right?)  If that iPart is just data on a table, with no model file of its own saved out to disk, that's a different story, and this code may not work for that scenario.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 14

Anonymous
Not applicable

no problem. thankyou very mutch

 

0 Likes
Message 8 of 14

luca.lauretti
Community Visitor
Community Visitor

I have very similar request.

I have an ipart model with different variants and i want to create only one IDW with different views for every variant of ipart.

The best thing is to copy a view for variant 1 ipart model, then paste the view and then change into variant  2.

The process goes until all variants are showing.

Thanks for your approciate. 

0 Likes
Message 9 of 14

WCrihfield
Mentor
Mentor

Just to clarify the situation...

  • You would be starting the iLogic rule from an 'iPart Factory' part document (the one with the complete table of variations defined within it), right?
  • And you want this rule to create a new drawing that is to be used, not open an existing drawing, right?
    • What path & file name do you want to save it as?  This data will need to be specified in the rule.
  • Will there just be one iPart variation shown per sheet, or multiple?  If multiple, how many?
  • How many views of the one iPart do you want the rule to create on each sheet?
    • If more than one view per sheet, then please specify which types of views, what orientations, where they should be on the sheet, view settings such as shaded/hidden lines, representation settings, etc.
  • Do actual separate model documents/files (iPart Members) exist in the file system for each variation within the iPart Factory?
    • If so, we may need to specify the directory path in the rule somewhere, so we can specify full file name (FullPath\name.extension) of each file to replace the one being referenced in the copied drawing view.

Needless to say, this could likely be a large and complex task, depending on your needs/wants and how detailed/specific everything needs to be.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 14

luca.lauretti
Community Visitor
Community Visitor

i have just created ipart with some different variants.

lucalauretti_0-1631198076813.png

 

I create an idw file with a view number 1 that contains ipart variation number 1.

lucalauretti_1-1631198189046.png

I want a macro that crate a new view (in the same idw file and sheet) with variation number 2.

But this new view has to be a copy of view number 1 with varition number 1 of ipart.

lucalauretti_2-1631198296935.png

I can do manually but the variations are too much, about 100...

So can i do a macro to do this work automatica?

0 Likes
Message 11 of 14

A.Acheson
Mentor
Mentor

As @WCrihfield  has mentioned this can be as complex or easy as you make it. This would be much easier to achieve if it were using one member per drawing file as already discussed. Why not 100 drawing files macro to save as and let a batch tool update the rev blocks, iproperties etc, and then PDF no need for complexity of the view placement etc? 

 

Are you using 1 member per sheet or multiple? This is where the difficulty will come in spacing out multiple views of different members on the same sheet. I would suggest 1 member per sheet but 100 sheets might get hard to handle in one drawing file due the file size.  

 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 12 of 14

bseerapathy
Participant
Participant

Hi,

My request is similar and for simple. it's a simple ipart with numerous members. I have created idw based on the first member. I need drgs to be created for other members. could be separate idw. or in separate sheets of single idw. The solution You gave I tried. Bot it's only on assembly documents.

I made an example

my ipart name is box.ipt

i have 4 members

box-1.ipt, box-2.ipt, box-3.ipt, box-4.ipt

I created box-1.idw with views and dimensions 

all I need idw for other 3.

 

Your assistance could be a great help.

 

0 Likes
Message 13 of 14

WCrihfield
Mentor
Mentor

Hi @bseerapathy.  Are all the iPart members the same exact geometry, but just different sizes, or do some members have 'features' that other members do not have (or Suppressed features)?  If the geometry is the same for all members, but just different sizes, then that would make this process the simplest scenario.  In that case, we could just 'copy' the existing drawing sheet, then paste it back into the same drawing document, as a new sheet, so that it already has the view and details in it.  Then just switch the DrawingView_ActiveMemberName property value so that it is pointing to the 'next' iPart member.  Some view scaling (DrawingView.Scale or DrawingView.ScaleString) may be required too, if there are large size differences among the other members, to keep the view fit within the sheet.  Then repeat those steps until we have a Sheet for each iPart member.  At least in theory.  There is not a 'built-in' method for copying a sheet within a single drawing document though, just one for copying a Sheet from one drawing document to another drawing document (Sheet.CopyTo method).  And the process of creating a 'New', temporary other drawing document, just for that copying process, then trying to do the sheet copying that way usually leads to multiple possible unexpected negative issues.  The steps required are super simple manually...just select a sheet, press Ctrl+C on keyboard, then unselect that Sheet, and press Ctrl+V on keyboard.  So, we would have to 'simulate' those manual actions by using code based object selections, and command executions, instead of true API methods.

Below is an example custom Function routine for copying a sheet within the same drawing document that I had likely posted elsewhere in this forum also.  If any of the views on the sheet have 'parent' views on another sheet, this method will not work to copy it though.  There are other, further developed versions of this code process, which check for things like that, and may even offer optionally assigning a name to the new sheet, or optionally create a specified number of copies of the specified sheet, but this example below is a simpler version, just to represent the main idea and process.

Sub Main
	Dim oDDoc As DrawingDocument = TryCast(ThisApplication.ActiveDocument, Inventor.DrawingDocument)
	If oDDoc Is Nothing Then Return
	Dim oOrigSheet As Inventor.Sheet = oDDoc.ActiveSheet
	Dim oNewSheet As Inventor.Sheet = CopySheet(oDDoc, oOrigSheet)
End Sub

Function CopySheet(oDDoc As DrawingDocument, oSheet As Inventor.Sheet) As Inventor.Sheet
	Dim oSS As SelectSet = oDDoc.SelectSet
	oSS.Clear()
	Dim oCM As CommandManager = ThisApplication.CommandManager
	Dim oCDs As ControlDefinitions = oCM.ControlDefinitions
	Dim oCopy As ControlDefinition = oCDs.Item("AppCopyCmd")
	Dim oPaste As ControlDefinition = oCDs.Item("AppPasteCmd")
	oSS.Select(oSheet)
	oCopy.Execute2(True)
	oSS.Clear()
	oPaste.Execute2(True)
	Return oDDoc.Sheets.Item(oDDoc.Sheets.Count)
End Function

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 14 of 14

bseerapathy
Participant
Participant

Hi, Thanks for the reply.

I have various iparts. few are simple .. and few are with feature suppression.

All I want, is

1. the code to copy the existing idw of the first member, which already created (The member name and idw name will be same).

2. save it as the new idw.

3. replace the model reference of the view.

4. save

5. repeat till the last instance

 

I took some AIxternal help too.. but got stuck in a place.. I couldn't proceed further, where I need human intelligence.

I don't mind sharing it.

I ran the below code in my idw file. But it's not able to recognise my member as a part of iPart factory 😞

you can make any simple ipart , make an idw add the code and and give a try. its generic.

 

Sub Main()
    ' Get the active drawing document
    Dim oDrawDoc As DrawingDocument
    oDrawDoc = ThisApplication.ActiveDocument

    ' Get the referenced part document from the first view
    Dim oPartDoc As PartDocument
    Try
        Dim oView As DrawingView
        oView = oDrawDoc.ActiveSheet.DrawingViews.Item(1)
        oPartDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
    Catch
        MessageBox.Show("Error: No valid drawing view or referenced part found.", "Error")
        Exit Sub
    End Try

    ' Check if the part is an iPart factory
    If Not oPartDoc.ComponentDefinition.IsiPartFactory Then
        MessageBox.Show("The referenced document (" & oPartDoc.FullFileName & ") is not an iPart factory. Ensure the drawing references the iPart factory file, not a member file.", "Error")
        Exit Sub
    End If

    ' Get the iPart factory table
    Dim oFactory As iPartFactory
    oFactory = oPartDoc.ComponentDefinition.iPartFactory

    ' Verify that the factory has rows
    If oFactory.TableRows.Count = 0 Then
        MessageBox.Show("The iPart table is empty. Open the factory file and generate member files.", "Error")
        Exit Sub
    End If

    ' Get the path for saving drawings
    Dim oPath As String
    oPath = System.IO.Path.GetDirectoryName(oDrawDoc.FullFileName)

    ' Loop through each row in the iPart table
    Dim oRow As iPartTableRow
    For Each oRow In oFactory.TableRows
        ' Verify the member exists
        Dim oMemberFile As String
        oMemberFile = oRow.MemberPath
        If Not System.IO.File.Exists(oMemberFile) Then
            MessageBox.Show("Member file for " & oRow.MemberName & " does not exist at: " & oMemberFile & ". Regenerate member files in the factory.", "Warning")
            Continue For
        End If

        ' Activate the iPart member
        Try
            oFactory.DefaultRow = oRow
            oPartDoc.ComponentDefinition.iPart.ChangeRow(oRow.MemberName)
        Catch ex As Exception
            MessageBox.Show("Error switching to member: " & oRow.MemberName & vbCrLf & ex.Message, "Error")
            Continue For
        End Try

        ' Update the drawing
        Try
            oDrawDoc.Update
        Catch ex As Exception
            MessageBox.Show("Error updating drawing for member: " & oRow.MemberName & vbCrLf & ex.Message, "Error")
            Continue For
        End Try

        ' Generate a new file name for the drawing
        Dim oMemberName As String
        oMemberName = oRow.MemberName
        Dim oNewFileName As String
        oNewFileName = oPath & "\" & oMemberName & ".idw"

        ' Save a copy of the drawing
        Try
            oDrawDoc.SaveAs(oNewFileName, False)
        Catch ex As Exception
            MessageBox.Show("Error saving drawing for member: " & oMemberName & vbCrLf & ex.Message, "Error")
            Continue For
        End Try
    Next

    ' Revert to the first member
    Try
        oFactory.DefaultRow = oFactory.TableRows.Item(1)
        oPartDoc.ComponentDefinition.iPart.ChangeRow(oFactory.TableRows.Item(1).MemberName)
        oDrawDoc.Update
        oDrawDoc.Save
    Catch ex As Exception
        MessageBox.Show("Error reverting to first member: " & ex.Message, "Error")
    End Try

    MessageBox.Show("Drawings generated for all iPart members.", "Success")
End Sub

 

 

 

0 Likes