Please look at this code

Please look at this code

Anonymous
Not applicable
1,346 Views
18 Replies
Message 1 of 19

Please look at this code

Anonymous
Not applicable
I copy/pasted this code to get sheet metal extents from a post but all the syntax is mashed together (tyvm Adesk). I tried to clean it up but I get errors upon debub. Can someone plz look at it & let me know what I missed?
0 Likes
1,347 Views
18 Replies
Replies (18)
Message 2 of 19

Anonymous
Not applicable
I can't view your attached file. This site gives me an error.
can you email it to me, so I can look at it for you?
thanks

[email protected]

0 Likes
Message 3 of 19

Anonymous
Not applicable
Ok, maybe a zip file will work?
0 Likes
Message 4 of 19

Anonymous
Not applicable
Zip file still gave me errors. =\
Anyone else having this issue with attached files?


... but to not get this thread off topic.... back to the real issue here....
I received your email and responded... please give me some time to find out what's going on in your sample.

-Addam


and p.s. to everyone.... if you dont want your post to look jumbled... select the "Rich Text" tab and create your post.

easier on the eyes... thanks.
0 Likes
Message 5 of 19

Anonymous
Not applicable
http://discussion.autodesk.com/forums/thread.jspa?messageID=5531286

Mind you, the new forum software has screwed up all the new lines in the code!! Thank you Autodesk!
0 Likes
Message 6 of 19

Anonymous
Not applicable
I beleive that's the post I copy/pasted from. As I posted above it's all mashed up. Can someone plz post a .txt file of that code that isn't mangled?
0 Likes
Message 7 of 19

Anonymous
Not applicable
Try this -
(still not a perfect display even with the Rich Text editor)

Public Sub AutoSave()
Dim oFlatPattern As FlatPattern
Set oFlatPattern = ThisDocument.ComponentDefinition.FlatPattern ' Check to see if the flat exists.
If Not oFlatPattern Is Nothing Then ' Get the extent of the face.
Dim oExtent As Box
Set oExtent = oFlatPattern.Body.RangeBox ' Extract the width and length from the range.
Dim dLength As Double
Dim dWidth As Double
dLength = oExtent.MaxPoint.X - oExtent.MinPoint.X
dWidth = oExtent.MaxPoint.Y - oExtent.MinPoint.Y
' Convert these values into the document units.
' This will result in strings that are identical
' to the strings shown in the Extent dialog.
Dim oUOM As UnitsOfMeasure
Set oUOM = ThisDocument.UnitsOfMeasure
Dim strWidth As String
Dim strLength As String
strWidth = oUOM.GetStringFromValue(dWidth, kDefaultDisplayLengthUnits)
strLength = oUOM.GetStringFromValue(dLength, kDefaultDisplayLengthUnits)
' Write these string as custom properties. If the properties already
' exist, then just update them.
Dim oCustomPropSet As PropertySet
Set oCustomPropSet = ThisDocument.PropertySets.Item("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
On Error Resume Next
oCustomPropSet.Item("SheetMetal Width").Value = strWidth
If Err
Then Err.Clear
Call oCustomPropSet.Add(strWidth, "SheetMetal Width")
End if
oCustomPropSet.Item("SheetMetal Length").Value = strLength
If Err Then
Err.Clear
Call oCustomPropSet.Add(strLength, "SheetMetal Length")
End If
Else ' The flat doesn't exist so delete the properties, if they exist.
On Error Resume Next
Set oCustomPropSet = ThisDocument.PropertySets.Item("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
oCustomPropSet.Item("SheetMetal Width").Delete
oCustomPropSet.Item("SheetMetal Length").Delete
End If
End Edited by: petercharles on Oct 9, 2008 9:44 PM
0 Likes
Message 8 of 19

Anonymous
Not applicable
Thank you, I was able to compile the code, but when I run it I get
"Run-time error '16' - Expression too complex"
and it points to dLength = oExtent.MaxPoint.X - oExtent.MinPoint.X
Ideas?
0 Likes
Message 9 of 19

Anonymous
Not applicable

I would highly recommend not doing this in a
document automatic macro.  I have some recent postings on my blog that
discuss this and show how to create an add-in to do the same thing.  Having
said that I did clean up your macro so that it works now.  Now you can
convert it to an add-in. 🙂
--
Brian Ekins
Autodesk Inventor
API

style="FONT-SIZE: 11pt; COLOR: red; FONT-FAMILY: 'Calibri','sans-serif'; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA">
href="http://blogs.autodesk.com/modthemachine">
color=#800080>http://blogs.autodesk.com/modthemachi...
0 Likes
Message 10 of 19

Anonymous
Not applicable
Brian,

Is there any way to post "readable" code on this forum??
0 Likes
Message 11 of 19

Anonymous
Not applicable

I don't know about the recent changes to the
forum.  I'm just a user of the forum like all of you.  What are you
using the read the newsgroup?  I've inserted the code below that I added as
an attachment before.  I'm using Outlook Express and have the format set to
"Rich Text".  It looks good in my editor, we'll see what it looks like when
I post it.

 

Public Sub AutoSave()
    Dim
oFlatPattern As FlatPattern
    Set oFlatPattern =
ThisDocument.ComponentDefinition.FlatPattern
   

    ' Check to see if the flat exists.
   
If Not oFlatPattern Is Nothing
Then
        ' Get the extent of the
face.
        Dim oExtent As
Box
        Set oExtent =
oFlatPattern.Body.RangeBox
       

        ' Extract the width and length
from the range.
        Dim dLength As
Double
        Dim dWidth As
Double
        Dim dMaxX As
Double
        dMaxX =
oExtent.MaxPoint.X
        Dim dMinX As
Double
        dMinX =
oExtent.MinPoint.X
        dLength = dMaxX
- dMinX
       

        Dim dMaxY As
Double
        dMaxY =
oExtent.MaxPoint.Y
        Dim dMinY As
Double
        dMinY =
oExtent.MinPoint.Y
        dWidth = dMaxY
- dMinY
       

        ' Convert these values into the
document units.
        ' This will result
in strings that are identical
        ' to
the strings shown in the Extent
dialog.
        Dim oUOM As
UnitsOfMeasure
        Set oUOM =
ThisDocument.UnitsOfMeasure
        Dim
strWidth As String
        Dim strLength
As String
        strWidth =
oUOM.GetStringFromValue(dWidth,
kDefaultDisplayLengthUnits)
       
strLength = oUOM.GetStringFromValue(dLength,
kDefaultDisplayLengthUnits)
       

        ' Write these string as custom
properties. If the properties already ' exist, then just update
them.
        Dim oCustomPropSet As
PropertySet
        Set oCustomPropSet =
ThisDocument.PropertySets.Item("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
       
On Error Resume Next
        strWidth =
oCustomPropSet.Item("SheetMetal
Width").Value
       

        If Err
Then
           
Err.Clear
           
Call oCustomPropSet.Add(strWidth, "SheetMetal
Width")
        End
If
           

        strLength =
oCustomPropSet.Item("SheetMetal
Length").Value
       

        If Err
Then
           
Err.Clear
           
Call oCustomPropSet.Add(strLength, "SheetMetal
Length")
        End
If
    Else ' The flat doesn't exist so delete the properties,
if they exist.
        On Error Resume
Next
        Set oCustomPropSet =
ThisDocument.PropertySets.Item("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
       
oCustomPropSet.Item("SheetMetal
Width").Delete
       
oCustomPropSet.Item("SheetMetal Length").Delete
    End
If
End Sub

 

0 Likes
Message 12 of 19

ekinsb
Alumni
Alumni
The code in the posting looks good, both in Outlook Express and the web reader. Here's some other code created through the web interface.

Public Sub ThisIsJunk()
Dim oFlatPattern As FlatPattern
Set oFlatPattern = ThisDocument.ComponentDefinition.FlatPattern

' Check to see if the flat exists.
If Not oFlatPattern Is Nothing Then
' Get the extent of the face.
Dim oExtent As Box
Set oExtent = oFlatPattern.Body.RangeBox
End If
End Sub

-Brian

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 13 of 19

ekinsb
Alumni
Alumni
The last posting was posted through the web interface using "Plain Text" and it's obviously bad. Here's another attempt using "Rich Text".

Public Sub AutoSave()
Dim oFlatPattern As FlatPattern
Set oFlatPattern = ThisDocument.ComponentDefinition.FlatPattern

' Check to see if the flat exists.
If Not oFlatPattern Is Nothing Then
' Get the extent of the face.
Dim oExtent As Box
Set oExtent = oFlatPattern.Body.RangeBox
End If
End Sub


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 14 of 19

ekinsb
Alumni
Alumni
Rich text is better in that it left the lines but it still took out the spaces. I'll see what I can find out.

-Brian

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 15 of 19

Anonymous
Not applicable
I always use Firefox as I usually go from one site to another.
0 Likes
Message 16 of 19

Anonymous
Not applicable
Thanks Brian,

I followed your blog and adjusted the code accordingly, but I'm not exactly sure how to build the add-in. I understand creating batch files for register & unregister but not how to make the dll.

I have a couple of questions for the code. Can the code be changed so that you can specify the precision of the units (ie 100.000 to 100). Can the code be modified so that instead of iProperties being created, the values create reference parameters? Edited by: IOMATT on Oct 10, 2008 11:17 AM
0 Likes
Message 17 of 19

Anonymous
Not applicable

To create the dll use the Build command in the
Build menu.  It will be created in the bin directory of your project.
--

Brian Ekins
Autodesk Inventor API

href="http://blogs.autodesk.com/modthemachine">http://blogs.autodesk.com/modthemachine
0 Likes
Message 18 of 19

Anonymous
Not applicable
Ok, the 'Build' commands are all greyed out.

I also modified the code to adjust precision to '0'. I have the extents outputing millimeters but I want to omit the units. Is this possible?
0 Likes
Message 19 of 19

Anonymous
Not applicable
Ok nevermind. I was able to create a build after all. I also found some code that will output the values to parameters instead, so I can format the values the way I want. Thanks for all the help.

Public Sub AutoSave()


Dim oFlatPattern As FlatPattern
Set oFlatPattern = ThisDocument.ComponentDefinition.FlatPattern
' Check to see if the flat exists.
If Not oFlatPattern Is Nothing Then
' Get the extent of the face.
Dim oExtent As Box
Set oExtent = oFlatPattern.Body.RangeBox
' Extract the width and length from the range.
Dim dLength As Double
Dim dWidth As Double
Dim dMaxX As Double
dMaxX = oExtent.MaxPoint.X
Dim dMinX As Double
dMinX = oExtent.MinPoint.X
dLength = dMaxX - dMinX
Dim dMaxY As Double
dMaxY = oExtent.MaxPoint.Y
Dim dMinY As Double
dMinY = oExtent.MinPoint.Y
dWidth = dMaxY - dMinY

Dim LengthParameter As Inventor.Parameter
Set LengthParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("Length")
LengthParameter.Value = dLength

Dim WidthParameter As Inventor.Parameter
Set WidthParameter = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.Item("Width")
WidthParameter.Value = dWidth

End If

End Sub

0 Likes